├── EasySP
├── .gitignore
├── LICENSE
├── build.gradle
├── easysp
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src
│ │ ├── androidTest
│ │ └── java
│ │ │ └── com
│ │ │ └── heima
│ │ │ └── easysp
│ │ │ └── ExampleInstrumentedTest.java
│ │ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── heima
│ │ │ │ └── easysp
│ │ │ │ └── SharedPreferencesUtils.java
│ │ └── res
│ │ │ └── values
│ │ │ └── strings.xml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── heima
│ │ └── easysp
│ │ └── ExampleUnitTest.java
├── gradle.properties
├── gradle
│ └── wrapper
│ │ ├── gradle-wrapper.jar
│ │ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── sample
│ ├── .gitignore
│ ├── build.gradle
│ ├── proguard-rules.pro
│ └── src
│ │ ├── androidTest
│ │ └── java
│ │ │ └── com
│ │ │ └── heima
│ │ │ └── easyspdemo
│ │ │ └── ExampleInstrumentedTest.java
│ │ ├── main
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── heima
│ │ │ │ └── easyspdemo
│ │ │ │ └── EasySPActivity.java
│ │ └── res
│ │ │ ├── layout
│ │ │ └── activity_main.xml
│ │ │ ├── mipmap-hdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ ├── ic_launcher.png
│ │ │ └── ic_launcher_round.png
│ │ │ └── values
│ │ │ ├── colors.xml
│ │ │ ├── strings.xml
│ │ │ └── styles.xml
│ │ └── test
│ │ └── java
│ │ └── com
│ │ └── heima
│ │ └── easyspdemo
│ │ └── ExampleUnitTest.java
└── settings.gradle
└── README.md
/EasySP/.gitignore:
--------------------------------------------------------------------------------
1 | # Android Studio
2 | *.iml
3 | .idea
4 | /.idea/workspace.xml
5 | /.idea/libraries
6 |
7 | # Gradle files
8 | .gradle
9 | /build
10 |
11 | # Local configuration file (sdk path, etc)
12 | /local.properties
13 |
14 |
15 | # NDK
16 | obj/
17 |
18 | # OSX files
19 | .DS_Store
20 |
21 | # Android Studio captures folder
22 | /captures
23 |
24 | # External native build folder generated in Android Studio 2.2 and later
25 | .externalNativeBuild
26 |
27 | # built application files
28 | *.apk
29 | *.ap_
30 |
31 | # Windows thumbnail db
32 | Thumbs.db
33 |
34 | # generated files
35 | bin/
36 | gen/
37 | out/
38 |
39 | # keystore
40 | /app/keystore
41 | *.jks
42 | /keystore.properties
43 |
--------------------------------------------------------------------------------
/EasySP/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Wh1te
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
--------------------------------------------------------------------------------
/EasySP/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 | repositories {
5 | jcenter()
6 | }
7 | dependencies {
8 | classpath 'com.android.tools.build:gradle:2.2.2'
9 | classpath 'com.github.dcendents:android-maven-gradle-plugin:1.5'
10 |
11 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
12 | }
13 | }
14 |
15 | allprojects {
16 | repositories {
17 | jcenter()
18 | }
19 | }
20 |
21 | task clean(type: Delete) {
22 | delete rootProject.buildDir
23 | }
24 |
--------------------------------------------------------------------------------
/EasySP/easysp/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle/
2 | .idea/
3 | */build/
4 | build/
5 | gradle/
6 | local.properties
7 | *.iml
--------------------------------------------------------------------------------
/EasySP/easysp/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 | group='com.github.open-android'
5 |
6 |
7 | android {
8 | compileSdkVersion 25
9 | buildToolsVersion "25.0.0"
10 |
11 | defaultConfig {
12 | minSdkVersion 9
13 | targetSdkVersion 25
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
18 |
19 | }
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | }
27 |
28 | dependencies {
29 | compile fileTree(dir: 'libs', include: ['*.jar'])
30 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
31 | exclude group: 'com.android.support', module: 'support-annotations'
32 | })
33 | compile 'com.android.support:appcompat-v7:25.0.0'
34 | testCompile 'junit:junit:4.12'
35 | }
36 |
37 |
38 |
--------------------------------------------------------------------------------
/EasySP/easysp/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in G:\Android\adt-bundle-windows-x86_64-20140321\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/EasySP/easysp/src/androidTest/java/com/heima/easysp/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.heima.easysp;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.white.easysharedperfs.test", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/EasySP/easysp/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
5 |
6 |
10 |
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/EasySP/easysp/src/main/java/com/heima/easysp/SharedPreferencesUtils.java:
--------------------------------------------------------------------------------
1 | package com.heima.easysp;
2 | //
3 | // _ooOoo_
4 | // o8888888o
5 | // 88" . "88
6 | // (| -_- |)
7 | // O\ = /O
8 | // ____/`---'\____
9 | // .' \\| |// `.
10 | // / \\||| : |||// \
11 | // / _||||| -:- |||||- \
12 | // | | \\\ - /// | |
13 | // | \_| ''\---/'' | |
14 | // \ .-\__ `-` ___/-. /
15 | // ___`. .' /--.--\ `. . __
16 | // ."" '< `.___\_<|>_/___.' >'"".
17 | // | | : `- \`.;`\ _ /`;.`/ - ` : | |
18 | // \ \ `-. \_ __\ /__ _/ .-` / /
19 | // ======`-.____`-.___\_____/___.-`____.-'======
20 | // `=---='
21 | // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
22 | // 佛祖保佑 永无BUG
23 |
24 | // 佛曰:
25 | // 写字楼里写字间,写字间里程序员;
26 | // 程序人员写程序,又拿程序换酒钱。
27 | // 酒醒只在网上坐,酒醉还来网下眠;
28 | // 酒醉酒醒日复日,网上网下年复年。
29 | // 但愿老死电脑间,不愿鞠躬老板前;
30 | // 奔驰宝马贵者趣,公交自行程序员。
31 | // 别人笑我忒疯癫,我笑自己命太贱;
32 | // 不见满街漂亮妹,哪个归得程序员?
33 | //
34 |
35 | import android.content.Context;
36 | import android.content.SharedPreferences;
37 | import android.os.Build;
38 | import android.support.annotation.StringRes;
39 | import android.support.v4.content.SharedPreferencesCompat;
40 | import android.util.Log;
41 |
42 | import java.util.HashSet;
43 | import java.util.Map;
44 | import java.util.Set;
45 |
46 |
47 |
48 | public class SharedPreferencesUtils {
49 |
50 | private static final String TAG = "SharedPreferencesUtils";
51 |
52 | private static SharedPreferencesUtils sSharedPreferencesUtils;
53 |
54 | private static SharedPreferences sSharedPreferences;
55 | private static SharedPreferences.Editor sEditor;
56 | private static SharedPreferencesCompat.EditorCompat editorCompat = SharedPreferencesCompat.EditorCompat.getInstance();
57 |
58 | private static final String DEFAULT_SP_NAME = "SharedData";
59 | private static final int DEFAULT_INT = 0;
60 | private static final float DEFAULT_FLOAT = 0.0f;
61 | private static final String DEFAULT_STRING = "";
62 | private static final boolean DEFAULT_BOOLEAN = false;
63 | private static final Set DEFAULT_STRING_SET = new HashSet<>(0);
64 |
65 | private static String mCurSPName = DEFAULT_SP_NAME;
66 | private static Context mContext;
67 |
68 | private SharedPreferencesUtils(Context context) {
69 | this(context, DEFAULT_SP_NAME);
70 | }
71 |
72 | private SharedPreferencesUtils(Context context, String spName) {
73 | mContext = context.getApplicationContext();
74 | sSharedPreferences = mContext.getSharedPreferences(spName, Context.MODE_PRIVATE);
75 | sEditor = sSharedPreferences.edit();
76 | mCurSPName = spName;
77 | Log.i(TAG, "SharedPreferencesUtils: " + mCurSPName);
78 | }
79 |
80 | public static SharedPreferencesUtils init(Context context) {
81 | if (sSharedPreferencesUtils == null || !mCurSPName.equals(DEFAULT_SP_NAME)) {
82 | sSharedPreferencesUtils = new SharedPreferencesUtils(context);
83 | }
84 | return sSharedPreferencesUtils;
85 | }
86 |
87 | public static SharedPreferencesUtils init(Context context, String spName) {
88 | if (sSharedPreferencesUtils == null) {
89 | sSharedPreferencesUtils = new SharedPreferencesUtils(context, spName);
90 | } else if (!spName.equals(mCurSPName)) {
91 | sSharedPreferencesUtils = new SharedPreferencesUtils(context, spName);
92 | }
93 | return sSharedPreferencesUtils;
94 | }
95 |
96 | public SharedPreferencesUtils put(@StringRes int key, Object value) {
97 | return put(mContext.getString(key), value);
98 | }
99 |
100 | public SharedPreferencesUtils put(String key, Object value) {
101 |
102 | if (value instanceof String) {
103 | sEditor.putString(key, (String) value);
104 | } else if (value instanceof Integer) {
105 | sEditor.putInt(key, (Integer) value);
106 | } else if (value instanceof Boolean) {
107 | sEditor.putBoolean(key, (Boolean) value);
108 | } else if (value instanceof Float) {
109 | sEditor.putFloat(key, (Float) value);
110 | } else if (value instanceof Long) {
111 | sEditor.putLong(key, (Long) value);
112 | } else {
113 | sEditor.putString(key, value.toString());
114 | }
115 | editorCompat.apply(sEditor);
116 | return sSharedPreferencesUtils;
117 | }
118 |
119 | public Object get(@StringRes int key, Object defaultObject) {
120 | return get(mContext.getString(key), defaultObject);
121 | }
122 |
123 | public Object get(String key, Object defaultObject) {
124 | if (defaultObject instanceof String) {
125 | return sSharedPreferences.getString(key, (String) defaultObject);
126 | } else if (defaultObject instanceof Integer) {
127 | return sSharedPreferences.getInt(key, (int) defaultObject);
128 | } else if (defaultObject instanceof Boolean) {
129 | return sSharedPreferences.getBoolean(key, (boolean) defaultObject);
130 | } else if (defaultObject instanceof Float) {
131 | return sSharedPreferences.getFloat(key, (float) defaultObject);
132 | } else if (defaultObject instanceof Long) {
133 | return sSharedPreferences.getLong(key, (long) defaultObject);
134 | }
135 | return null;
136 | }
137 |
138 | public SharedPreferencesUtils putInt(String key, int value) {
139 | sEditor.putInt(key, value);
140 | editorCompat.apply(sEditor);
141 | return this;
142 | }
143 |
144 | public SharedPreferencesUtils putInt(@StringRes int key, int value) {
145 | return putInt(mContext.getString(key), value);
146 | }
147 |
148 | public int getInt(@StringRes int key) {
149 | return getInt(mContext.getString(key));
150 | }
151 |
152 | public int getInt(@StringRes int key, int defValue) {
153 | return getInt(mContext.getString(key), defValue);
154 | }
155 |
156 | public int getInt(String key) {
157 | return getInt(key, DEFAULT_INT);
158 | }
159 |
160 |
161 | public int getInt(String key, int defValue) {
162 | return sSharedPreferences.getInt(key, defValue);
163 | }
164 |
165 | public SharedPreferencesUtils putFloat(@StringRes int key, float value) {
166 | return putFloat(mContext.getString(key), value);
167 | }
168 |
169 | public SharedPreferencesUtils putFloat(String key, float value) {
170 | sEditor.putFloat(key, value);
171 | editorCompat.apply(sEditor);
172 | return sSharedPreferencesUtils;
173 | }
174 |
175 | public float getFloat(String key) {
176 | return getFloat(key, DEFAULT_FLOAT);
177 | }
178 |
179 | public float getFloat(String key, float defValue) {
180 | return sSharedPreferences.getFloat(key, defValue);
181 | }
182 |
183 | public float getFloat(@StringRes int key) {
184 | return getFloat(mContext.getString(key));
185 | }
186 |
187 | public float getFloat(@StringRes int key, float defValue) {
188 | return getFloat(mContext.getString(key), defValue);
189 | }
190 |
191 | public SharedPreferencesUtils putLong(@StringRes int key, long value) {
192 | return putLong(mContext.getString(key), value);
193 | }
194 |
195 | public SharedPreferencesUtils putLong(String key, long value) {
196 | sEditor.putLong(key, value);
197 | editorCompat.apply(sEditor);
198 | return sSharedPreferencesUtils;
199 | }
200 |
201 | public long getLong(String key) {
202 | return getLong(key, DEFAULT_INT);
203 | }
204 |
205 | public long getLong(String key, long defValue) {
206 | return sSharedPreferences.getLong(key, defValue);
207 | }
208 |
209 | public long getLong(@StringRes int key) {
210 | return getLong(mContext.getString(key));
211 | }
212 |
213 | public long getLong(@StringRes int key, long defValue) {
214 | return getLong(mContext.getString(key), defValue);
215 | }
216 |
217 | public SharedPreferencesUtils putString(@StringRes int key, String value) {
218 | return putString(mContext.getString(key), value);
219 | }
220 |
221 | public SharedPreferencesUtils putString(String key, String value) {
222 | sEditor.putString(key, value);
223 | editorCompat.apply(sEditor);
224 | return sSharedPreferencesUtils;
225 | }
226 |
227 | public String getString(String key) {
228 | return getString(key, DEFAULT_STRING);
229 | }
230 |
231 | public String getString(String key, String defValue) {
232 | return sSharedPreferences.getString(key, defValue);
233 | }
234 |
235 | public String getString(@StringRes int key) {
236 | return getString(mContext.getString(key), DEFAULT_STRING);
237 | }
238 |
239 | public String getString(@StringRes int key, String defValue) {
240 | return getString(mContext.getString(key), defValue);
241 | }
242 |
243 | public SharedPreferencesUtils putBoolean(@StringRes int key, boolean value) {
244 | return putBoolean(mContext.getString(key), value);
245 | }
246 |
247 | public SharedPreferencesUtils putBoolean(String key, boolean value) {
248 | sEditor.putBoolean(key, value);
249 | editorCompat.apply(sEditor);
250 | return sSharedPreferencesUtils;
251 | }
252 |
253 | public boolean getBoolean(String key) {
254 | return getBoolean(key, DEFAULT_BOOLEAN);
255 | }
256 |
257 | public boolean getBoolean(String key, boolean defValue) {
258 | return sSharedPreferences.getBoolean(key, defValue);
259 | }
260 |
261 | public boolean getBoolean(@StringRes int key) {
262 | return getBoolean(mContext.getString(key));
263 | }
264 |
265 | public boolean getBoolean(@StringRes int key, boolean defValue) {
266 | return getBoolean(mContext.getString(key), defValue);
267 | }
268 |
269 | public SharedPreferencesUtils putStringSet(@StringRes int key, Set value) {
270 | return putStringSet(mContext.getString(key), value);
271 | }
272 |
273 | public SharedPreferencesUtils putStringSet(String key, Set value) {
274 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
275 | sEditor.putStringSet(key, value);
276 | editorCompat.apply(sEditor);
277 | }
278 | return sSharedPreferencesUtils;
279 | }
280 |
281 | public Set getStringSet(String key) {
282 | return getStringSet(key, DEFAULT_STRING_SET);
283 | }
284 |
285 |
286 | public Set getStringSet(String key, Set defValue) {
287 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
288 | return sSharedPreferences.getStringSet(key, defValue);
289 | } else {
290 | return DEFAULT_STRING_SET;
291 | }
292 | }
293 |
294 | public Set getStringSet(@StringRes int key) {
295 | return getStringSet(mContext.getString(key));
296 | }
297 |
298 | public Set getStringSet(@StringRes int key, Set defValue) {
299 | return getStringSet(mContext.getString(key), defValue);
300 | }
301 |
302 |
303 | public boolean contains(String key) {
304 | return sSharedPreferences.contains(key);
305 | }
306 |
307 | public boolean contains(@StringRes int key) {
308 | return contains(mContext.getString(key));
309 | }
310 |
311 | public Map getAll() {
312 | return sSharedPreferences.getAll();
313 | }
314 |
315 | public SharedPreferencesUtils remove(@StringRes int key) {
316 | return remove(mContext.getString(key));
317 | }
318 |
319 | public SharedPreferencesUtils remove(String key) {
320 | sEditor.remove(key);
321 | editorCompat.apply(sEditor);
322 | return sSharedPreferencesUtils;
323 | }
324 |
325 | public SharedPreferencesUtils clear() {
326 | sEditor.clear();
327 | editorCompat.apply(sEditor);
328 | return sSharedPreferencesUtils;
329 | }
330 |
331 | public SharedPreferences getSharedPreferences() {
332 | return sSharedPreferences;
333 | }
334 |
335 | }
336 |
--------------------------------------------------------------------------------
/EasySP/easysp/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | EasySharedPerfs
3 |
4 |
--------------------------------------------------------------------------------
/EasySP/easysp/src/test/java/com/heima/easysp/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.heima.easysp;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/EasySP/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 |
--------------------------------------------------------------------------------
/EasySP/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/EasySP/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue Feb 28 19:43:14 CST 2017
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-3.3-all.zip
7 |
--------------------------------------------------------------------------------
/EasySP/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 |
--------------------------------------------------------------------------------
/EasySP/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 |
--------------------------------------------------------------------------------
/EasySP/sample/.gitignore:
--------------------------------------------------------------------------------
1 | .gradle/
2 | .idea/
3 | */build/
4 | build/
5 | gradle/
6 | local.properties
7 | *.iml
--------------------------------------------------------------------------------
/EasySP/sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 25
5 | buildToolsVersion "25.0.0"
6 | defaultConfig {
7 | applicationId "com.heima.easysp"
8 | minSdkVersion 15
9 | targetSdkVersion 25
10 | versionCode 1
11 | versionName "1.0"
12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
13 | }
14 | buildTypes {
15 | release {
16 | minifyEnabled false
17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
18 | }
19 | }
20 | }
21 |
22 | dependencies {
23 | compile fileTree(include: ['*.jar'], dir: 'libs')
24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
25 | exclude group: 'com.android.support', module: 'support-annotations'
26 | })
27 | compile 'com.android.support:appcompat-v7:25.0.0'
28 | compile 'com.android.support.constraint:constraint-layout:1.0.0-beta4'
29 | testCompile 'junit:junit:4.12'
30 | compile project(':easysp')
31 | }
32 |
--------------------------------------------------------------------------------
/EasySP/sample/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # By default, the flags in this file are appended to flags specified
3 | # in G:\Android\adt-bundle-windows-x86_64-20140321\sdk/tools/proguard/proguard-android.txt
4 | # You can edit the include path and order by changing the proguardFiles
5 | # directive in build.gradle.
6 | #
7 | # For more details, see
8 | # http://developer.android.com/guide/developing/tools/proguard.html
9 |
10 | # Add any project specific keep options here:
11 |
12 | # If your project uses WebView with JS, uncomment the following
13 | # and specify the fully qualified class name to the JavaScript interface
14 | # class:
15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
16 | # public *;
17 | #}
18 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/EasySP/sample/src/androidTest/java/com/heima/easyspdemo/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package com.heima.easyspdemo;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumentation test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() throws Exception {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("com.white.easysp", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/EasySP/sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/EasySP/sample/src/main/java/com/heima/easyspdemo/EasySPActivity.java:
--------------------------------------------------------------------------------
1 | package com.heima.easyspdemo;
2 |
3 | import android.os.Bundle;
4 | import android.support.v7.app.AppCompatActivity;
5 | import android.util.Log;
6 | import android.view.View;
7 | import android.widget.Button;
8 | import android.widget.TextView;
9 |
10 | import com.heima.easysp.SharedPreferencesUtils;
11 |
12 | import java.util.HashSet;
13 | import java.util.Set;
14 |
15 | public class EasySPActivity extends AppCompatActivity {
16 |
17 | public static final String TAG = "EasySPActivity";
18 |
19 | private Button mBtnPutAll;
20 | private Button mBtnPutOne;
21 | private Button mBtnGet;
22 | private Button mBtnClear;
23 | private TextView mTvGet;
24 |
25 | @Override
26 | protected void onCreate(Bundle savedInstanceState) {
27 | super.onCreate(savedInstanceState);
28 | setContentView(R.layout.activity_main);
29 | mBtnPutAll = (Button) findViewById(R.id.btn_put_all);
30 | mBtnPutOne = (Button) findViewById(R.id.btn_put_one);
31 | mBtnGet = (Button) findViewById(R.id.btn_get);
32 | mBtnClear = (Button) findViewById(R.id.btn_clear);
33 | mTvGet = (TextView) findViewById(R.id.tv_get);
34 |
35 | mBtnPutAll.setOnClickListener(new View.OnClickListener() {
36 | @Override
37 | public void onClick(View v) {
38 | Set strings = new HashSet<>();
39 | strings.add("set str1");
40 | strings.add("set str2");
41 | strings.add("set str3");
42 |
43 | SharedPreferencesUtils.init(EasySPActivity.this)
44 | .putBoolean(R.string.key_bool, true)
45 | .putInt(R.string.key_int, 1)
46 | .putString(R.string.key_string, "string")
47 | .putLong(R.string.key_long, 1000000000)
48 | .putFloat(R.string.key_float, 1.1f)
49 | .put("put", 100)
50 | .putStringSet(R.string.key_set, strings);
51 | }
52 | });
53 |
54 | mBtnPutOne.setOnClickListener(new View.OnClickListener() {
55 | @Override
56 | public void onClick(View v) {
57 | SharedPreferencesUtils.init(EasySPActivity.this).putString("one", "String one");
58 | }
59 | });
60 |
61 | mBtnGet.setOnClickListener(new View.OnClickListener() {
62 | @Override
63 | public void onClick(View v) {
64 | SharedPreferencesUtils sharedPreferencesUtils = SharedPreferencesUtils.init(EasySPActivity.this);
65 | Log.i(TAG, "contains: " + sharedPreferencesUtils.contains("bool"));
66 | StringBuilder sb = new StringBuilder("SharedPreferencesData:\n");
67 | sb.append("bool:").append(sharedPreferencesUtils.getBoolean(R.string.key_bool)).append("\n")
68 | .append("int:").append(sharedPreferencesUtils.getInt(R.string.key_int)).append("\n")
69 | .append("str:").append(sharedPreferencesUtils.getString(R.string.key_string)).append("\n")
70 | .append("long:").append(sharedPreferencesUtils.getLong(R.string.key_long)).append("\n")
71 | .append("float:").append(sharedPreferencesUtils.getFloat(R.string.key_float)).append("\n")
72 | .append("one:").append(sharedPreferencesUtils.getString("one")).append("\n");
73 | Set stringSet = sharedPreferencesUtils.getStringSet(R.string.key_set);
74 | for (String str : stringSet) {
75 | sb.append("set:").append(str).append("\n");
76 | }
77 | mTvGet.setText(sb.toString());
78 | }
79 | });
80 |
81 | mBtnClear.setOnClickListener(new View.OnClickListener() {
82 | @Override
83 | public void onClick(View v) {
84 | SharedPreferencesUtils.init(EasySPActivity.this).clear();
85 | }
86 | });
87 |
88 | }
89 | }
90 |
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
25 |
26 |
33 |
34 |
41 |
42 |
49 |
50 |
51 |
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/open-android/SharedPreferencesUtils/2b6cef2945207fd859c2ac73e00f25d6a6958e5a/EasySP/sample/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | EasySP
3 | bool
4 | int
5 | float
6 | long
7 | string
8 | set
9 |
10 |
--------------------------------------------------------------------------------
/EasySP/sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/EasySP/sample/src/test/java/com/heima/easyspdemo/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package com.heima.easyspdemo;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() throws Exception {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/EasySP/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':sample', ':easysp'
2 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | 欢迎关注微信公众号、长期为您推荐优秀博文、开源项目、视频
3 |
4 | 微信公众号:Android干货程序员
5 |
6 | 
7 |
8 | ## 使用步骤
9 |
10 | ### 1. 在project的build.gradle添加如下代码(如下图)
11 |
12 | allprojects {
13 | repositories {
14 | ...
15 | maven { url "https://jitpack.io" }
16 | }
17 | }
18 |
19 | ### 2. 在Module的build.gradle添加依赖
20 | ```
21 | dependencies {
22 | compile 'com.github.open-android:SharedPreferencesUtils:0.1.0
23 | }
24 | ```
25 |
26 |
27 | - 保存数据到sharedpreferences
28 | ```
29 | SharedPreferencesUtils.init(context).putBoolean("boolean", true);
30 |
31 | //可以使用字符串资源作为保存的键
32 | SharedPreferencesUtils.init(context).putBoolean(R.string.key_bool, true);
33 |
34 | //可以自定义sharedpreferences的文件名
35 | SharedPreferencesUtils.init(context,"Custom").putBoolean("boolean", true);
36 | ```
37 |
38 | - 一句代码保存多条数据(链式调用)
39 | ```
40 | SharedPreferencesUtils.init(context)
41 | .putBoolean(R.string.key_bool, true)
42 | .putInt(R.string.key_int, 1)
43 | .putString(R.string.key_string, "string")
44 | .putLong(R.string.key_long, 1000000000)
45 | .putFloat(R.string.key_float, 1.1f)
46 | .put("put", 100)
47 | .putStringSet(R.string.key_set, strings);
48 | ```
49 |
50 | - 读取数据
51 | ```
52 | boolean booleanData = SharedPreferencesUtils.init(context).getBoolean("bool");
53 |
54 | boolean booleanData = SharedPreferencesUtils.init(context).getBoolean(R.string.key_bool,defValue);
55 | ```
56 |
57 | - 移除某个键对应的数据
58 | ```
59 | SharedPreferencesUtils.init(context).remove(bool);
60 |
61 | SharedPreferencesUtils.init(context).remove(R.string.key_bool);
62 | ```
63 | - 清除所有数据
64 | ```
65 | SharedPreferencesUtils.init(context).clear();
66 | ```
67 |
68 |
69 |
--------------------------------------------------------------------------------