├── .gitignore
├── .idea
├── encodings.xml
├── gradle.xml
├── misc.xml
├── modules.xml
├── runConfigurations.xml
└── vcs.xml
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── preferencesproviderlibrary
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── zlm
│ │ └── libs
│ │ └── preferences
│ │ ├── PreferencesProvider.java
│ │ ├── PreferencesProviderUtils.java
│ │ └── PreferencesUtils.java
│ └── res
│ └── values
│ └── strings.xml
├── readme.md
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 | .DS_Store
7 | /build
8 | /captures
9 | .externalNativeBuild
10 |
--------------------------------------------------------------------------------
/.idea/encodings.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
--------------------------------------------------------------------------------
/.idea/modules.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.0.0'
11 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
12 |
13 |
14 | // NOTE: Do not place your application dependencies here; they belong
15 | // in the individual module build.gradle files
16 | }
17 | }
18 |
19 | allprojects {
20 | repositories {
21 | google()
22 | jcenter()
23 | maven { url 'https://jitpack.io' }
24 | }
25 | }
26 |
27 | task clean(type: Delete) {
28 | delete rootProject.buildDir
29 | }
30 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zhangliangming/PreferencesProvider/8a089e76034ad5867b3c8c9a901510417e34d6b1/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Sun Apr 29 16:23:58 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.1-all.zip
7 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env bash
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
10 | DEFAULT_JVM_OPTS=""
11 |
12 | APP_NAME="Gradle"
13 | APP_BASE_NAME=`basename "$0"`
14 |
15 | # Use the maximum available, or set MAX_FD != -1 to use that value.
16 | MAX_FD="maximum"
17 |
18 | warn ( ) {
19 | echo "$*"
20 | }
21 |
22 | die ( ) {
23 | echo
24 | echo "$*"
25 | echo
26 | exit 1
27 | }
28 |
29 | # OS specific support (must be 'true' or 'false').
30 | cygwin=false
31 | msys=false
32 | darwin=false
33 | case "`uname`" in
34 | CYGWIN* )
35 | cygwin=true
36 | ;;
37 | Darwin* )
38 | darwin=true
39 | ;;
40 | MINGW* )
41 | msys=true
42 | ;;
43 | esac
44 |
45 | # Attempt to set APP_HOME
46 | # Resolve links: $0 may be a link
47 | PRG="$0"
48 | # Need this for relative symlinks.
49 | while [ -h "$PRG" ] ; do
50 | ls=`ls -ld "$PRG"`
51 | link=`expr "$ls" : '.*-> \(.*\)$'`
52 | if expr "$link" : '/.*' > /dev/null; then
53 | PRG="$link"
54 | else
55 | PRG=`dirname "$PRG"`"/$link"
56 | fi
57 | done
58 | SAVED="`pwd`"
59 | cd "`dirname \"$PRG\"`/" >/dev/null
60 | APP_HOME="`pwd -P`"
61 | cd "$SAVED" >/dev/null
62 |
63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
64 |
65 | # Determine the Java command to use to start the JVM.
66 | if [ -n "$JAVA_HOME" ] ; then
67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
68 | # IBM's JDK on AIX uses strange locations for the executables
69 | JAVACMD="$JAVA_HOME/jre/sh/java"
70 | else
71 | JAVACMD="$JAVA_HOME/bin/java"
72 | fi
73 | if [ ! -x "$JAVACMD" ] ; then
74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
75 |
76 | Please set the JAVA_HOME variable in your environment to match the
77 | location of your Java installation."
78 | fi
79 | else
80 | JAVACMD="java"
81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
82 |
83 | Please set the JAVA_HOME variable in your environment to match the
84 | location of your Java installation."
85 | fi
86 |
87 | # Increase the maximum file descriptors if we can.
88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
89 | MAX_FD_LIMIT=`ulimit -H -n`
90 | if [ $? -eq 0 ] ; then
91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
92 | MAX_FD="$MAX_FD_LIMIT"
93 | fi
94 | ulimit -n $MAX_FD
95 | if [ $? -ne 0 ] ; then
96 | warn "Could not set maximum file descriptor limit: $MAX_FD"
97 | fi
98 | else
99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
100 | fi
101 | fi
102 |
103 | # For Darwin, add options to specify how the application appears in the dock
104 | if $darwin; then
105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
106 | fi
107 |
108 | # For Cygwin, switch paths to Windows format before running java
109 | if $cygwin ; then
110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
112 | JAVACMD=`cygpath --unix "$JAVACMD"`
113 |
114 | # We build the pattern for arguments to be converted via cygpath
115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
116 | SEP=""
117 | for dir in $ROOTDIRSRAW ; do
118 | ROOTDIRS="$ROOTDIRS$SEP$dir"
119 | SEP="|"
120 | done
121 | OURCYGPATTERN="(^($ROOTDIRS))"
122 | # Add a user-defined pattern to the cygpath arguments
123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
125 | fi
126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
127 | i=0
128 | for arg in "$@" ; do
129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
131 |
132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
134 | else
135 | eval `echo args$i`="\"$arg\""
136 | fi
137 | i=$((i+1))
138 | done
139 | case $i in
140 | (0) set -- ;;
141 | (1) set -- "$args0" ;;
142 | (2) set -- "$args0" "$args1" ;;
143 | (3) set -- "$args0" "$args1" "$args2" ;;
144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
150 | esac
151 | fi
152 |
153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules
154 | function splitJvmOpts() {
155 | JVM_OPTS=("$@")
156 | }
157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"
159 |
160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
161 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
12 | set DEFAULT_JVM_OPTS=
13 |
14 | set DIRNAME=%~dp0
15 | if "%DIRNAME%" == "" set DIRNAME=.
16 | set APP_BASE_NAME=%~n0
17 | set APP_HOME=%DIRNAME%
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windowz variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 | if "%@eval[2+2]" == "4" goto 4NT_args
53 |
54 | :win9xME_args
55 | @rem Slurp the command line arguments.
56 | set CMD_LINE_ARGS=
57 | set _SKIP=2
58 |
59 | :win9xME_args_slurp
60 | if "x%~1" == "x" goto execute
61 |
62 | set CMD_LINE_ARGS=%*
63 | goto execute
64 |
65 | :4NT_args
66 | @rem Get arguments from the 4NT Shell from JP Software
67 | set CMD_LINE_ARGS=%$
68 |
69 | :execute
70 | @rem Setup the command line
71 |
72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
73 |
74 | @rem Execute Gradle
75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
76 |
77 | :end
78 | @rem End local scope for the variables with windows NT shell
79 | if "%ERRORLEVEL%"=="0" goto mainEnd
80 |
81 | :fail
82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
83 | rem the _cmd.exe /c_ return code!
84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
85 | exit /b 1
86 |
87 | :mainEnd
88 | if "%OS%"=="Windows_NT" endlocal
89 |
90 | :omega
91 |
--------------------------------------------------------------------------------
/preferencesproviderlibrary/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/preferencesproviderlibrary/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | group = 'com.github.zhangliangming'
4 | android {
5 | compileSdkVersion 26
6 | defaultConfig {
7 |
8 | minSdkVersion 19
9 | targetSdkVersion 26
10 | versionCode 1
11 | versionName "1.0"
12 |
13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
14 |
15 | }
16 | buildTypes {
17 | release {
18 | zipAlignEnabled true
19 | minifyEnabled true
20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
21 | }
22 | }
23 | productFlavors {
24 | }
25 | }
26 |
27 | dependencies {
28 | }
29 |
--------------------------------------------------------------------------------
/preferencesproviderlibrary/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
23 | -keep class com.zlm.libs.preferences.**
24 | -keepclassmembers class com.zlm.libs.preferences.** {
25 | public *;
26 | }
--------------------------------------------------------------------------------
/preferencesproviderlibrary/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/preferencesproviderlibrary/src/main/java/com/zlm/libs/preferences/PreferencesProvider.java:
--------------------------------------------------------------------------------
1 | package com.zlm.libs.preferences;
2 |
3 | import android.content.ContentProvider;
4 | import android.content.ContentValues;
5 | import android.content.Context;
6 | import android.content.SharedPreferences;
7 | import android.content.UriMatcher;
8 | import android.database.Cursor;
9 | import android.database.MatrixCursor;
10 | import android.net.Uri;
11 | import android.text.TextUtils;
12 |
13 | import java.util.Set;
14 |
15 | /**
16 | * @Description: ContentProvider
17 | * @author: zhangliangming
18 | * @date: 2018-04-29 16:39
19 | **/
20 | public abstract class PreferencesProvider extends ContentProvider {
21 |
22 | private UriMatcher mUriMatcher;
23 |
24 | /**
25 | * 表列名
26 | */
27 | public static String COLUMNNAME = "SPCOLUMNNAME";
28 | /**
29 | * authorities key
30 | */
31 | public static String AUTHORITIES_KEY = "authorities_key";
32 | /**
33 | * authorities_spname
34 | */
35 | public static String AUTHORITIES_SPNAME = "authorities_spname";
36 | /**
37 | * string
38 | */
39 | private String mStringPath = "string/*/*/";
40 | public static final int STRING_CONTENT_URI_CODE = 100;
41 | /**
42 | * int
43 | */
44 | private String mIntegerPath = "integer/*/*/";
45 | public static final int INTEGER_CONTENT_URI_CODE = 101;
46 | /**
47 | * long
48 | */
49 | private String mLongPath = "long/*/*/";
50 | public static final int LONG_CONTENT_URI_CODE = 102;
51 | /**
52 | * float
53 | */
54 | private String mFloatPath = "float/*/*/";
55 | public static final int FLOAT_CONTENT_URI_CODE = 104;
56 | /**
57 | * boolean
58 | */
59 | private String mBooleanPath = "boolean/*/*/";
60 | public static final int BOOLEAN_CONTENT_URI_CODE = 105;
61 |
62 | /**
63 | *
64 | */
65 | private String mDeletePath = "delete/*/*/";
66 | public static final int DELETE_CONTENT_URI_CODE = 106;
67 |
68 | /**
69 | *
70 | */
71 | private String mPutsPath = "puts";
72 | public static final int PUTS_CONTENT_URI_CODE = 107;
73 |
74 | public abstract String getAuthorities();
75 |
76 |
77 | @Override
78 | public boolean onCreate() {
79 |
80 | String authorities = getAuthorities();
81 | //保存authorities
82 | PreferencesUtils.putString(getContext(), AUTHORITIES_SPNAME, AUTHORITIES_KEY, authorities);
83 |
84 | mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
85 | mUriMatcher.addURI(authorities, mStringPath, STRING_CONTENT_URI_CODE);
86 | mUriMatcher.addURI(authorities, mStringPath + "*/", STRING_CONTENT_URI_CODE);
87 |
88 | mUriMatcher.addURI(authorities, mIntegerPath, INTEGER_CONTENT_URI_CODE);
89 | mUriMatcher.addURI(authorities, mIntegerPath + "*/", INTEGER_CONTENT_URI_CODE);
90 |
91 | mUriMatcher.addURI(authorities, mLongPath, LONG_CONTENT_URI_CODE);
92 | mUriMatcher.addURI(authorities, mLongPath + "*/", LONG_CONTENT_URI_CODE);
93 |
94 | mUriMatcher.addURI(authorities, mFloatPath, FLOAT_CONTENT_URI_CODE);
95 | mUriMatcher.addURI(authorities, mFloatPath + "*/", FLOAT_CONTENT_URI_CODE);
96 |
97 | mUriMatcher.addURI(authorities, mBooleanPath, BOOLEAN_CONTENT_URI_CODE);
98 | mUriMatcher.addURI(authorities, mBooleanPath + "*/", BOOLEAN_CONTENT_URI_CODE);
99 |
100 | mUriMatcher.addURI(authorities, mDeletePath, DELETE_CONTENT_URI_CODE);
101 |
102 | mUriMatcher.addURI(authorities, mPutsPath, PUTS_CONTENT_URI_CODE);
103 |
104 | return false;
105 | }
106 |
107 | @Override
108 | public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
109 | Model model = getModel(uri);
110 | if (model == null) return null;
111 | int code = mUriMatcher.match(uri);
112 | Cursor cursor = buildCursor(getContext(), model, code);
113 | return cursor;
114 | }
115 |
116 | @Override
117 | public String getType(Uri uri) {
118 | return null;
119 | }
120 |
121 | @Override
122 | public Uri insert(Uri uri, ContentValues values) {
123 | Model model = getModel(uri);
124 | if (model == null) return null;
125 | int code = mUriMatcher.match(uri);
126 | if (code == STRING_CONTENT_URI_CODE || code == INTEGER_CONTENT_URI_CODE || code == LONG_CONTENT_URI_CODE
127 | || code == FLOAT_CONTENT_URI_CODE || code == BOOLEAN_CONTENT_URI_CODE || code == PUTS_CONTENT_URI_CODE) {
128 | insert(getContext(), values, model);
129 | }
130 | return uri;
131 | }
132 |
133 | @Override
134 | public int delete(Uri uri, String selection, String[] selectionArgs) {
135 | Model model = getModel(uri);
136 | if (model == null) return -1;
137 | int code = mUriMatcher.match(uri);
138 | if (code == STRING_CONTENT_URI_CODE || code == INTEGER_CONTENT_URI_CODE || code == LONG_CONTENT_URI_CODE
139 | || code == FLOAT_CONTENT_URI_CODE || code == BOOLEAN_CONTENT_URI_CODE) {
140 | delete(getContext(), model);
141 | }
142 |
143 | return 0;
144 | }
145 |
146 | @Override
147 | public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
148 | Model model = getModel(uri);
149 | if (model == null) return -1;
150 | int code = mUriMatcher.match(uri);
151 | if (code == STRING_CONTENT_URI_CODE || code == INTEGER_CONTENT_URI_CODE || code == LONG_CONTENT_URI_CODE
152 | || code == FLOAT_CONTENT_URI_CODE || code == BOOLEAN_CONTENT_URI_CODE) {
153 | insert(getContext(), values, model);
154 | }
155 | return 0;
156 | }
157 |
158 | /**
159 | * 删除
160 | *
161 | * @param context
162 | * @param model
163 | */
164 | private void delete(Context context, Model model) {
165 | SharedPreferences.Editor editor = PreferencesUtils.getEditor(context, model.getSpName());
166 | editor.remove(model.getKey());
167 | editor.apply();
168 | }
169 |
170 | /**
171 | * 插入数据
172 | *
173 | * @param context
174 | * @param values
175 | * @param model
176 | */
177 | private void insert(Context context, ContentValues values, Model model) {
178 | SharedPreferences.Editor editor = PreferencesUtils.getEditor(context, model.getSpName());
179 | Set keys = values.keySet();
180 | for (String key : keys) {
181 | Object value = values.get(key);
182 | if (value instanceof Integer) {
183 | editor.putInt(key, Integer.parseInt(value + ""));
184 | } else if (value instanceof Long) {
185 | editor.putLong(key, Long.parseLong(value + ""));
186 | } else if (value instanceof Float) {
187 | editor.putFloat(key, Float.parseFloat(value + ""));
188 | } else if (value instanceof Boolean) {
189 | editor.putBoolean(key, Boolean.valueOf(value + ""));
190 | } else {
191 | editor.putString(key, (value == null ? "" : value) + "");
192 | }
193 | }
194 | editor.apply();
195 | }
196 |
197 | /**
198 | * 从sp中获取数据
199 | *
200 | * @return
201 | */
202 | private Cursor buildCursor(Context context, Model model, int code) {
203 | Object value = null;
204 | Object defValue = model.getDefValue();
205 | switch (code) {
206 | case STRING_CONTENT_URI_CODE:
207 |
208 | if (defValue == null) {
209 | value = PreferencesUtils.getString(context, model.getSpName(), model.getKey());
210 | } else {
211 | value = PreferencesUtils.getString(context, model.getSpName(), model.getKey(), String.valueOf(defValue));
212 | }
213 |
214 | break;
215 | case INTEGER_CONTENT_URI_CODE:
216 | if (defValue == null) {
217 | value = PreferencesUtils.getInt(context, model.getSpName(), model.getKey());
218 | } else {
219 | if (!TextUtils.isDigitsOnly(defValue + "")) {
220 | defValue = -1;
221 | }
222 | value = PreferencesUtils.getInt(context, model.getSpName(), model.getKey(), Integer.parseInt(defValue + ""));
223 | }
224 |
225 | break;
226 | case LONG_CONTENT_URI_CODE:
227 |
228 | if (defValue == null) {
229 | value = PreferencesUtils.getLong(context, model.getSpName(), model.getKey());
230 | } else {
231 | if (!TextUtils.isDigitsOnly(defValue + "")) {
232 | defValue = -1;
233 | }
234 | value = PreferencesUtils.getLong(context, model.getSpName(), model.getKey(), Long.parseLong(defValue + ""));
235 | }
236 |
237 | break;
238 | case FLOAT_CONTENT_URI_CODE:
239 |
240 | if (defValue == null) {
241 | value = PreferencesUtils.getFloat(context, model.getSpName(), model.getKey());
242 | } else {
243 |
244 | value = PreferencesUtils.getFloat(context, model.getSpName(), model.getKey(), Float.parseFloat(defValue + ""));
245 | }
246 |
247 | break;
248 | case BOOLEAN_CONTENT_URI_CODE:
249 |
250 | if (defValue == null) {
251 | value = PreferencesUtils.getBoolean(context, model.getSpName(), model.getKey()) + "";
252 | } else {
253 | value = PreferencesUtils.getBoolean(context, model.getSpName(), model.getKey(), Boolean.valueOf(defValue + "")) + "";
254 | }
255 |
256 | break;
257 | default:
258 | break;
259 | }
260 | if (value == null) return null;
261 | //
262 | String[] columnNames = {COLUMNNAME};
263 | MatrixCursor cursor = new MatrixCursor(columnNames);
264 | Object[] values = {value};
265 | cursor.addRow(values);
266 | return cursor;
267 | }
268 |
269 | /**
270 | * 从uri中获取spname和key
271 | *
272 | * @param uri
273 | * @return
274 | */
275 | private Model getModel(Uri uri) {
276 | try {
277 |
278 | Model model = new Model();
279 | model.setSpName(uri.getPathSegments().get(1));
280 | if (uri.getPathSegments().size() > 2) {
281 | model.setKey(uri.getPathSegments().get(2));
282 | }
283 | if (uri.getPathSegments().size() > 3) {
284 | model.setDefValue(uri.getPathSegments().get(3));
285 | }
286 |
287 | return model;
288 | } catch (Exception e) {
289 | e.printStackTrace();
290 | }
291 | return null;
292 | }
293 |
294 | /**
295 | *
296 | */
297 | private class Model {
298 | private String spName;
299 | private String key;
300 | private Object defValue;
301 |
302 | public String getSpName() {
303 | return spName;
304 | }
305 |
306 | public void setSpName(String spName) {
307 | this.spName = spName;
308 | }
309 |
310 | public String getKey() {
311 | return key;
312 | }
313 |
314 | public void setKey(String key) {
315 | this.key = key;
316 | }
317 |
318 | public Object getDefValue() {
319 | return defValue;
320 | }
321 |
322 | public void setDefValue(Object defValue) {
323 | this.defValue = defValue;
324 | }
325 | }
326 | }
327 |
--------------------------------------------------------------------------------
/preferencesproviderlibrary/src/main/java/com/zlm/libs/preferences/PreferencesProviderUtils.java:
--------------------------------------------------------------------------------
1 | package com.zlm.libs.preferences;
2 |
3 | import android.content.ContentResolver;
4 | import android.content.ContentValues;
5 | import android.content.Context;
6 | import android.database.Cursor;
7 | import android.net.Uri;
8 |
9 | /**
10 | * @Description: PreferencesProviderUtils
11 | * @author: zhangliangming
12 | * @date: 2018-04-29 19:07
13 | **/
14 | public class PreferencesProviderUtils {
15 |
16 | /**
17 | * put string preferences
18 | *
19 | * @param context
20 | * @param key The name of the preference to modify
21 | * @param value The new value for the preference
22 | * @return True if the new values were successfully written to persistent storage.
23 | */
24 | public static boolean putString(Context context, String spName, String key, String value) {
25 | Uri uri = buildUri(context, PreferencesProvider.STRING_CONTENT_URI_CODE, spName, key, value);
26 | ContentResolver cr = context.getContentResolver();
27 | try {
28 | ContentValues values = new ContentValues();
29 | values.put(key, value);
30 |
31 | cr.insert(uri, values);
32 | return true;
33 | } catch (Exception e) {
34 | e.printStackTrace();
35 | }
36 | return false;
37 |
38 | }
39 |
40 | /**
41 | * 移除
42 | *
43 | * @param context
44 | * @param spName
45 | * @param key
46 | * @return
47 | */
48 | public static boolean remove(Context context, String spName, String key) {
49 | try {
50 | Uri uri = buildUri(context, PreferencesProvider.DELETE_CONTENT_URI_CODE, spName, key, null);
51 | ContentResolver cr = context.getContentResolver();
52 |
53 | cr.delete(uri, null, null);
54 |
55 | return true;
56 | } catch (Exception e) {
57 | e.printStackTrace();
58 | }
59 | return false;
60 |
61 | }
62 |
63 | /**
64 | * get string preferences
65 | *
66 | * @param context
67 | * @param key The name of the preference to retrieve
68 | * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this
69 | * name that is not a string
70 | * @see #getString(Context, String, String)
71 | */
72 | public static String getString(Context context, String spName, String key) {
73 | return getString(context, spName, key, "");
74 | }
75 |
76 | /**
77 | * get string preferences
78 | *
79 | * @param context
80 | * @param key The name of the preference to retrieve
81 | * @param defaultValue Value to return if this preference does not exist
82 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
83 | * this name that is not a string
84 | */
85 | public static String getString(Context context, String spName, String key, String defaultValue) {
86 | String result = defaultValue;
87 | Uri uri = buildUri(context, PreferencesProvider.STRING_CONTENT_URI_CODE, spName, key, defaultValue);
88 | ContentResolver cr = context.getContentResolver();
89 | Cursor cursor = cr.query(uri, null, null, null, null);
90 | if (cursor == null) return result;
91 | if (cursor.moveToNext()) {
92 | result = cursor.getString(cursor.getColumnIndex(PreferencesProvider.COLUMNNAME));
93 | }
94 |
95 | return result;
96 | }
97 |
98 | /**
99 | * put int preferences
100 | *
101 | * @param context
102 | * @param key The name of the preference to modify
103 | * @param value The new value for the preference
104 | * @return True if the new values were successfully written to persistent storage.
105 | */
106 | public static boolean putInt(Context context, String spName, String key, int value) {
107 | Uri uri = buildUri(context, PreferencesProvider.INTEGER_CONTENT_URI_CODE, spName, key, value);
108 | ContentResolver cr = context.getContentResolver();
109 | try {
110 | ContentValues values = new ContentValues();
111 | values.put(key, value);
112 |
113 | cr.insert(uri, values);
114 | return true;
115 | } catch (Exception e) {
116 | e.printStackTrace();
117 | }
118 | return false;
119 | }
120 |
121 | /**
122 | * get int preferences
123 | *
124 | * @param context
125 | * @param key The name of the preference to retrieve
126 | * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
127 | * name that is not a int
128 | * @see #getInt(Context, String, String, int)
129 | */
130 | public static int getInt(Context context, String spName, String key) {
131 | return getInt(context, spName, key, -1);
132 | }
133 |
134 | /**
135 | * get int preferences
136 | *
137 | * @param context
138 | * @param key The name of the preference to retrieve
139 | * @param defaultValue Value to return if this preference does not exist
140 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
141 | * this name that is not a int
142 | */
143 | public static int getInt(Context context, String spName, String key, int defaultValue) {
144 | int result = defaultValue;
145 | Uri uri = buildUri(context, PreferencesProvider.INTEGER_CONTENT_URI_CODE, spName, key, defaultValue);
146 | ContentResolver cr = context.getContentResolver();
147 | Cursor cursor = cr.query(uri, null, null, null, null);
148 | if (cursor == null) return result;
149 |
150 | if (cursor.moveToNext()) {
151 | result = cursor.getInt(cursor.getColumnIndex(PreferencesProvider.COLUMNNAME));
152 |
153 | }
154 | return result;
155 | }
156 |
157 | /**
158 | * put long preferences
159 | *
160 | * @param context
161 | * @param key The name of the preference to modify
162 | * @param value The new value for the preference
163 | * @return True if the new values were successfully written to persistent storage.
164 | */
165 | public static boolean putLong(Context context, String spName, String key, long value) {
166 | Uri uri = buildUri(context, PreferencesProvider.LONG_CONTENT_URI_CODE, spName, key, value);
167 | ContentResolver cr = context.getContentResolver();
168 | try {
169 | ContentValues values = new ContentValues();
170 | values.put(key, value);
171 |
172 | cr.insert(uri, values);
173 | return true;
174 | } catch (Exception e) {
175 | e.printStackTrace();
176 | }
177 | return false;
178 | }
179 |
180 | /**
181 | * get long preferences
182 | *
183 | * @param context
184 | * @param key The name of the preference to retrieve
185 | * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
186 | * name that is not a long
187 | * @see #getLong(Context, String, String, long)
188 | */
189 | public static long getLong(Context context, String spName, String key) {
190 | return getLong(context, spName, key, -1);
191 | }
192 |
193 | /**
194 | * get long preferences
195 | *
196 | * @param context
197 | * @param key The name of the preference to retrieve
198 | * @param defaultValue Value to return if this preference does not exist
199 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
200 | * this name that is not a long
201 | */
202 | public static long getLong(Context context, String spName, String key, long defaultValue) {
203 | long result = defaultValue;
204 | Uri uri = buildUri(context, PreferencesProvider.LONG_CONTENT_URI_CODE, spName, key, defaultValue);
205 | ContentResolver cr = context.getContentResolver();
206 | Cursor cursor = cr.query(uri, null, null, null, null);
207 | if (cursor == null) return result;
208 |
209 | if (cursor.moveToNext()) {
210 | result = cursor.getLong(cursor.getColumnIndex(PreferencesProvider.COLUMNNAME));
211 | }
212 |
213 | return result;
214 | }
215 |
216 | /**
217 | * put float preferences
218 | *
219 | * @param context
220 | * @param key The name of the preference to modify
221 | * @param value The new value for the preference
222 | * @return True if the new values were successfully written to persistent storage.
223 | */
224 | public static boolean putFloat(Context context, String spName, String key, float value) {
225 | Uri uri = buildUri(context, PreferencesProvider.FLOAT_CONTENT_URI_CODE, spName, key, value);
226 | ContentResolver cr = context.getContentResolver();
227 | try {
228 | ContentValues values = new ContentValues();
229 | values.put(key, value);
230 |
231 | cr.insert(uri, values);
232 | return true;
233 | } catch (Exception e) {
234 | e.printStackTrace();
235 | }
236 | return false;
237 | }
238 |
239 | /**
240 | * get float preferences
241 | *
242 | * @param context
243 | * @param key The name of the preference to retrieve
244 | * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
245 | * name that is not a float
246 | * @see #getFloat(Context, String, String, float)
247 | */
248 | public static float getFloat(Context context, String spName, String key) {
249 | return getFloat(context, spName, key, -1);
250 | }
251 |
252 | /**
253 | * get float preferences
254 | *
255 | * @param context
256 | * @param key The name of the preference to retrieve
257 | * @param defaultValue Value to return if this preference does not exist
258 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
259 | * this name that is not a float
260 | */
261 | public static float getFloat(Context context, String spName, String key, float defaultValue) {
262 | float result = defaultValue;
263 | Uri uri = buildUri(context, PreferencesProvider.FLOAT_CONTENT_URI_CODE, spName, key, defaultValue);
264 | ContentResolver cr = context.getContentResolver();
265 | Cursor cursor = cr.query(uri, null, null, null, null);
266 | if (cursor == null) return result;
267 |
268 | if (cursor.moveToNext()) {
269 | result = cursor.getFloat(cursor.getColumnIndex(PreferencesProvider.COLUMNNAME));
270 | }
271 |
272 | return result;
273 | }
274 |
275 | /**
276 | * put boolean preferences
277 | *
278 | * @param context
279 | * @param key The name of the preference to modify
280 | * @param value The new value for the preference
281 | * @return True if the new values were successfully written to persistent storage.
282 | */
283 | public static boolean putBoolean(Context context, String spName, String key, boolean value) {
284 | Uri uri = buildUri(context, PreferencesProvider.BOOLEAN_CONTENT_URI_CODE, spName, key, value);
285 | ContentResolver cr = context.getContentResolver();
286 | try {
287 | ContentValues values = new ContentValues();
288 | values.put(key, value);
289 |
290 | cr.insert(uri, values);
291 | return true;
292 | } catch (Exception e) {
293 | e.printStackTrace();
294 | }
295 | return false;
296 | }
297 |
298 | /**
299 | * get boolean preferences, default is false
300 | *
301 | * @param context
302 | * @param key The name of the preference to retrieve
303 | * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this
304 | * name that is not a boolean
305 | * @see #getBoolean(Context, String, String, boolean)
306 | */
307 | public static boolean getBoolean(Context context, String spName, String key) {
308 | return getBoolean(context, spName, key, false);
309 | }
310 |
311 | /**
312 | * get boolean preferences
313 | *
314 | * @param context
315 | * @param key The name of the preference to retrieve
316 | * @param defaultValue Value to return if this preference does not exist
317 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
318 | * this name that is not a boolean
319 | */
320 | public static boolean getBoolean(Context context, String spName, String key, boolean defaultValue) {
321 | boolean result = defaultValue;
322 | Uri uri = buildUri(context, PreferencesProvider.BOOLEAN_CONTENT_URI_CODE, spName, key, defaultValue);
323 | ContentResolver cr = context.getContentResolver();
324 | Cursor cursor = cr.query(uri, null, null, null, null);
325 | if (cursor == null) return result;
326 |
327 | if (cursor.moveToNext()) {
328 | result = Boolean.valueOf(cursor.getString(cursor.getColumnIndex(PreferencesProvider.COLUMNNAME)));
329 | }
330 |
331 | return result;
332 | }
333 |
334 | /**
335 | * @param context
336 | * @param spName
337 | * @param datas
338 | * @return
339 | */
340 | public static boolean put(Context context, String spName, ContentValues datas) {
341 | Uri uri = buildUri(context, PreferencesProvider.PUTS_CONTENT_URI_CODE, spName, null, null);
342 | ContentResolver cr = context.getContentResolver();
343 | try {
344 |
345 | cr.insert(uri, datas);
346 | return true;
347 | } catch (Exception e) {
348 | e.printStackTrace();
349 | }
350 | return false;
351 | }
352 |
353 | /**
354 | * @param code
355 | * @param key
356 | * @param value
357 | * @return
358 | */
359 | private static Uri buildUri(Context context, int code, String spName, String key, Object value) {
360 | String authorities = PreferencesUtils.getString(context, PreferencesProvider.AUTHORITIES_SPNAME, PreferencesProvider.AUTHORITIES_KEY);
361 | Uri uri = null;
362 |
363 | switch (code) {
364 | case PreferencesProvider.STRING_CONTENT_URI_CODE:
365 | uri = Uri
366 | .parse("content://" + authorities + "/" + "string/" + spName + "/" + key + "/" + value);
367 |
368 | break;
369 | case PreferencesProvider.INTEGER_CONTENT_URI_CODE:
370 |
371 | uri = Uri
372 | .parse("content://" + authorities + "/" + "integer/" + spName + "/" + key + "/" + value);
373 |
374 |
375 | break;
376 | case PreferencesProvider.LONG_CONTENT_URI_CODE:
377 |
378 | uri = Uri
379 | .parse("content://" + authorities + "/" + "long/" + spName + "/" + key + "/" + value);
380 |
381 |
382 | break;
383 | case PreferencesProvider.FLOAT_CONTENT_URI_CODE:
384 |
385 | uri = Uri
386 | .parse("content://" + authorities + "/" + "float/" + spName + "/" + key + "/" + value);
387 |
388 |
389 | break;
390 | case PreferencesProvider.BOOLEAN_CONTENT_URI_CODE:
391 |
392 | uri = Uri
393 | .parse("content://" + authorities + "/" + "boolean/" + spName + "/" + key + "/" + value);
394 |
395 |
396 | break;
397 |
398 | case PreferencesProvider.DELETE_CONTENT_URI_CODE:
399 |
400 | uri = Uri
401 | .parse("content://" + authorities + "/" + "delete/" + spName + "/" + key);
402 |
403 |
404 | break;
405 |
406 | case PreferencesProvider.PUTS_CONTENT_URI_CODE:
407 |
408 | uri = Uri
409 | .parse("content://" + authorities + "/" + "puts");
410 |
411 |
412 | break;
413 |
414 | default:
415 | break;
416 | }
417 | return uri;
418 | }
419 | }
420 |
--------------------------------------------------------------------------------
/preferencesproviderlibrary/src/main/java/com/zlm/libs/preferences/PreferencesUtils.java:
--------------------------------------------------------------------------------
1 | package com.zlm.libs.preferences;
2 |
3 | import android.content.Context;
4 | import android.content.SharedPreferences;
5 |
6 | /**
7 | * PreferencesUtils, easy to get or put data
8 | *
9 | * Preference Name
10 | * you can change preference name by {@link #}
11 | *
12 | *
13 | * Put Value
14 | * put string {@link #putString(Context, String, String, String)}
15 | * put int {@link #putInt(Context, String, String, int)}
16 | * put long {@link #putLong(Context, String, String, long)}
17 | * put float {@link #putFloat(Context, String, String, float)}
18 | * put boolean {@link #putBoolean(Context, String, String, boolean)}
19 | *
20 | *
21 | * Get Value
22 | * get string {@link #getString(Context, String, String)}, {@link #getString(Context, String, String)}
23 | * get int {@link #getInt(Context, String, String)}, {@link #getInt(Context, String, String, int)}
24 | * get long {@link #getLong(Context, String, String)}, {@link #getLong(Context, String, String, long)}
25 | * get float {@link #getFloat(Context, String, String)}, {@link #getFloat(Context, String, String, float)}
26 | * get boolean {@link #getBoolean(Context, String, String)}, {@link #getBoolean(Context, String, String, boolean)}
27 | *
28 | *
29 | * @author Trinea 2013-3-6
30 | */
31 | public class PreferencesUtils {
32 | /**
33 | * put string preferences
34 | *
35 | * @param context
36 | * @param key The name of the preference to modify
37 | * @param value The new value for the preference
38 | * @return True if the new values were successfully written to persistent storage.
39 | */
40 | public static boolean putString(Context context, String spName, String key, String value) {
41 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
42 | SharedPreferences.Editor editor = settings.edit();
43 | editor.putString(key, value);
44 | return editor.commit();
45 | }
46 |
47 | public static boolean remove(Context context, String spName, String key) {
48 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
49 | SharedPreferences.Editor editor = settings.edit();
50 | editor.remove(key);
51 | return editor.commit();
52 | }
53 |
54 | /**
55 | * get string preferences
56 | *
57 | * @param context
58 | * @param key The name of the preference to retrieve
59 | * @return The preference value if it exists, or null. Throws ClassCastException if there is a preference with this
60 | * name that is not a string
61 | * @see #getString(Context, String, String)
62 | */
63 | public static String getString(Context context, String spName, String key) {
64 | return getString(context, spName, key, null);
65 | }
66 |
67 | /**
68 | * get string preferences
69 | *
70 | * @param context
71 | * @param key The name of the preference to retrieve
72 | * @param defaultValue Value to return if this preference does not exist
73 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
74 | * this name that is not a string
75 | */
76 | public static String getString(Context context, String spName, String key, String defaultValue) {
77 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
78 | return settings.getString(key, defaultValue);
79 | }
80 |
81 | /**
82 | * put int preferences
83 | *
84 | * @param context
85 | * @param key The name of the preference to modify
86 | * @param value The new value for the preference
87 | * @return True if the new values were successfully written to persistent storage.
88 | */
89 | public static boolean putInt(Context context, String spName, String key, int value) {
90 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
91 | SharedPreferences.Editor editor = settings.edit();
92 | editor.putInt(key, value);
93 | return editor.commit();
94 | }
95 |
96 | /**
97 | * get int preferences
98 | *
99 | * @param context
100 | * @param key The name of the preference to retrieve
101 | * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
102 | * name that is not a int
103 | * @see #getInt(Context, String, String, int)
104 | */
105 | public static int getInt(Context context, String spName, String key) {
106 | return getInt(context, spName, key, -1);
107 | }
108 |
109 | /**
110 | * get int preferences
111 | *
112 | * @param context
113 | * @param key The name of the preference to retrieve
114 | * @param defaultValue Value to return if this preference does not exist
115 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
116 | * this name that is not a int
117 | */
118 | public static int getInt(Context context, String spName, String key, int defaultValue) {
119 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
120 | return settings.getInt(key, defaultValue);
121 | }
122 |
123 | /**
124 | * put long preferences
125 | *
126 | * @param context
127 | * @param key The name of the preference to modify
128 | * @param value The new value for the preference
129 | * @return True if the new values were successfully written to persistent storage.
130 | */
131 | public static boolean putLong(Context context, String spName, String key, long value) {
132 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
133 | SharedPreferences.Editor editor = settings.edit();
134 | editor.putLong(key, value);
135 | return editor.commit();
136 | }
137 |
138 | /**
139 | * get long preferences
140 | *
141 | * @param context
142 | * @param key The name of the preference to retrieve
143 | * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
144 | * name that is not a long
145 | * @see #getLong(Context, String, String, long)
146 | */
147 | public static long getLong(Context context, String spName, String key) {
148 | return getLong(context, spName, key, -1);
149 | }
150 |
151 | /**
152 | * get long preferences
153 | *
154 | * @param context
155 | * @param key The name of the preference to retrieve
156 | * @param defaultValue Value to return if this preference does not exist
157 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
158 | * this name that is not a long
159 | */
160 | public static long getLong(Context context, String spName, String key, long defaultValue) {
161 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
162 | return settings.getLong(key, defaultValue);
163 | }
164 |
165 | /**
166 | * put float preferences
167 | *
168 | * @param context
169 | * @param key The name of the preference to modify
170 | * @param value The new value for the preference
171 | * @return True if the new values were successfully written to persistent storage.
172 | */
173 | public static boolean putFloat(Context context, String spName, String key, float value) {
174 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
175 | SharedPreferences.Editor editor = settings.edit();
176 | editor.putFloat(key, value);
177 | return editor.commit();
178 | }
179 |
180 | /**
181 | *
182 | * @param context
183 | * @param spName
184 | * @return
185 | */
186 | public static SharedPreferences.Editor getEditor(Context context, String spName){
187 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
188 | SharedPreferences.Editor editor = settings.edit();
189 | return editor;
190 | }
191 |
192 | /**
193 | * get float preferences
194 | *
195 | * @param context
196 | * @param key The name of the preference to retrieve
197 | * @return The preference value if it exists, or -1. Throws ClassCastException if there is a preference with this
198 | * name that is not a float
199 | * @see #getFloat(Context, String, String, float)
200 | */
201 | public static float getFloat(Context context, String spName, String key) {
202 | return getFloat(context, spName, key, -1);
203 | }
204 |
205 | /**
206 | * get float preferences
207 | *
208 | * @param context
209 | * @param key The name of the preference to retrieve
210 | * @param defaultValue Value to return if this preference does not exist
211 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
212 | * this name that is not a float
213 | */
214 | public static float getFloat(Context context, String spName, String key, float defaultValue) {
215 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
216 | return settings.getFloat(key, defaultValue);
217 | }
218 |
219 | /**
220 | * put boolean preferences
221 | *
222 | * @param context
223 | * @param key The name of the preference to modify
224 | * @param value The new value for the preference
225 | * @return True if the new values were successfully written to persistent storage.
226 | */
227 | public static boolean putBoolean(Context context, String spName, String key, boolean value) {
228 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
229 | SharedPreferences.Editor editor = settings.edit();
230 | editor.putBoolean(key, value);
231 | return editor.commit();
232 | }
233 |
234 | /**
235 | * get boolean preferences, default is false
236 | *
237 | * @param context
238 | * @param key The name of the preference to retrieve
239 | * @return The preference value if it exists, or false. Throws ClassCastException if there is a preference with this
240 | * name that is not a boolean
241 | * @see #getBoolean(Context, String, String, boolean)
242 | */
243 | public static boolean getBoolean(Context context, String spName, String key) {
244 | return getBoolean(context, spName, key, false);
245 | }
246 |
247 | /**
248 | * get boolean preferences
249 | *
250 | * @param context
251 | * @param key The name of the preference to retrieve
252 | * @param defaultValue Value to return if this preference does not exist
253 | * @return The preference value if it exists, or defValue. Throws ClassCastException if there is a preference with
254 | * this name that is not a boolean
255 | */
256 | public static boolean getBoolean(Context context, String spName, String key, boolean defaultValue) {
257 | SharedPreferences settings = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
258 | return settings.getBoolean(key, defaultValue);
259 | }
260 |
261 | }
262 |
--------------------------------------------------------------------------------
/preferencesproviderlibrary/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | PreferencesProvider
3 |
4 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | # 简介 #
2 | 之前项目使用了SharedPreferences来保存数据,然而项目修改为多进程后,SharedPreferences保存数据却出现了数据没有更新的问题,当然SharedPreferences设置了MODE_MULTI_PROCESS后可以使数据同步,不过官方已经废弃了原先的MODE_MULTI_PROCESS, 并且建议跨进程存取值还是用ContentProvider之类的更靠谱一些,由于ContentProvider提供了对底层数据存储方式的抽象,底层我们可以使用SQLite,MongoDB等等,当然也可以使用SharedPreferences来实现
3 |
4 | ## v1.3 ##
5 | - 2018-05-05
6 | - 添加混淆
7 |
8 | ## v1.2 ##
9 | - 2018-05-02
10 | - 添加put多个key和value接口
11 |
12 |
13 | ## v1.0 ##
14 |
15 |
16 | - 2018-04-30
17 | - ContentProvider封装SharedPreferences功能,解决跨进程存取值的问题
18 |
19 | # Gradle #
20 | 1.root build.gradle
21 |
22 | `allprojects {
23 | repositories {
24 | ...
25 | maven { url 'https://jitpack.io' }
26 | }
27 | }`
28 |
29 | 2.app build.gradle
30 |
31 | `dependencies {
32 | compile 'com.github.zhangliangming:PreferencesProvider:v1.3'
33 | }`
34 |
35 | # 调用Demo #
36 |
37 | 链接: [https://pan.baidu.com/s/15SixU_nviX1ppK74gxL3dg](https://pan.baidu.com/s/15SixU_nviX1ppK74gxL3dg) 密码: u8hw
38 |
39 | # 调用用法 #
40 |
41 | ## 继承 ##
42 | 
43 | ## 声明 ##
44 | 
45 | ## 相关API ##
46 | 
47 | 
48 |
49 |
50 | # 参考 #
51 |
52 | [ContentProvider从入门到精通](https://www.jianshu.com/p/f5ec75a9cfea)
53 |
54 | [http://bbs.51cto.com/thread-1070974-1.html](http://bbs.51cto.com/thread-1070974-1.html)
55 |
56 |
57 | # 捐赠 #
58 | 如果该项目对您有所帮助,欢迎您的赞赏
59 |
60 | - 微信
61 |
62 | 
63 |
64 | - 支付宝
65 |
66 | 
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':preferencesproviderlibrary'
2 |
--------------------------------------------------------------------------------