|
3 | 3 | import android.content.Context; |
4 | 4 | import android.content.SharedPreferences; |
5 | 5 |
|
| 6 | +import java.util.Map; |
| 7 | + |
6 | 8 | /** |
7 | 9 | * <pre> |
8 | 10 | * author: Blankj |
9 | 11 | * blog : http://blankj.com |
10 | 12 | * time : 2016/8/2 |
11 | | - * desc : SP读写工具类 |
| 13 | + * desc : SP相关工具类 |
12 | 14 | * </pre> |
13 | 15 | */ |
14 | 16 | public class SPUtils { |
15 | 17 |
|
16 | | - private SPUtils() { |
17 | | - throw new UnsupportedOperationException("u can't fuck me..."); |
18 | | - } |
| 18 | + private SharedPreferences sp; |
| 19 | + private SharedPreferences.Editor editor; |
19 | 20 |
|
20 | 21 | /** |
21 | | - * SP的name值 |
22 | | - * <p>可通过修改PREFERENCE_NAME变量修改SP的name值</p> |
| 22 | + * SPUtils构造函数 |
| 23 | + * |
| 24 | + * @param context 上下文 |
| 25 | + * @param spName spName |
23 | 26 | */ |
24 | | - public static String PREFERENCE_NAME = "ANDROID_UTIL_CODE"; |
| 27 | + public SPUtils(Context context, String spName) { |
| 28 | + sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE); |
| 29 | + editor = sp.edit(); |
| 30 | + editor.apply(); |
| 31 | + } |
25 | 32 |
|
26 | 33 | /** |
27 | 34 | * SP中写入String类型value |
28 | 35 | * |
29 | | - * @param context 上下文 |
30 | | - * @param key 键 |
31 | | - * @param value 值 |
32 | | - * @return {@code true}: 写入成功<br>{@code false}: 写入失败 |
| 36 | + * @param key 键 |
| 37 | + * @param value 值 |
33 | 38 | */ |
34 | | - public static boolean putString(Context context, String key, String value) { |
35 | | - return getSP(context).edit().putString(key, value).commit(); |
| 39 | + public void putString(String key, String value) { |
| 40 | + editor.putString(key, value).apply(); |
36 | 41 | } |
37 | 42 |
|
38 | 43 | /** |
39 | 44 | * SP中读取String |
40 | 45 | * |
41 | | - * @param context 上下文 |
42 | | - * @param key 键 |
43 | | - * @return 存在返回对应值,不存在返回默认值null |
| 46 | + * @param key 键 |
| 47 | + * @return 存在返回对应值,不存在返回默认值{@code null} |
44 | 48 | */ |
45 | | - public static String getString(Context context, String key) { |
46 | | - return getString(context, key, null); |
| 49 | + public String getString(String key) { |
| 50 | + return getString(key, null); |
47 | 51 | } |
48 | 52 |
|
49 | 53 | /** |
50 | 54 | * SP中读取String |
51 | 55 | * |
52 | | - * @param context 上下文 |
53 | 56 | * @param key 键 |
54 | 57 | * @param defaultValue 默认值 |
55 | | - * @return 存在返回对应值,不存在返回默认值defaultValue |
| 58 | + * @return 存在返回对应值,不存在返回默认值{@code defaultValue} |
56 | 59 | */ |
57 | | - public static String getString(Context context, String key, String defaultValue) { |
58 | | - return getSP(context).getString(key, defaultValue); |
| 60 | + public String getString(String key, String defaultValue) { |
| 61 | + return sp.getString(key, defaultValue); |
59 | 62 | } |
60 | 63 |
|
61 | 64 | /** |
62 | 65 | * SP中写入int类型value |
63 | 66 | * |
64 | | - * @param context 上下文 |
65 | | - * @param key 键 |
66 | | - * @param value 值 |
67 | | - * @return {@code true}: 写入成功<br>{@code false}: 写入失败 |
| 67 | + * @param key 键 |
| 68 | + * @param value 值 |
68 | 69 | */ |
69 | | - public static boolean putInt(Context context, String key, int value) { |
70 | | - return getSP(context).edit().putInt(key, value).commit(); |
| 70 | + public void putInt(String key, int value) { |
| 71 | + editor.putInt(key, value).apply(); |
71 | 72 | } |
72 | 73 |
|
73 | 74 | /** |
74 | 75 | * SP中读取int |
75 | 76 | * |
76 | | - * @param context 上下文 |
77 | | - * @param key 键 |
| 77 | + * @param key 键 |
78 | 78 | * @return 存在返回对应值,不存在返回默认值-1 |
79 | 79 | */ |
80 | | - public static int getInt(Context context, String key) { |
81 | | - return getInt(context, key, -1); |
| 80 | + public int getInt(String key) { |
| 81 | + return getInt(key, -1); |
82 | 82 | } |
83 | 83 |
|
84 | 84 | /** |
85 | 85 | * SP中读取int |
86 | 86 | * |
87 | | - * @param context 上下文 |
88 | 87 | * @param key 键 |
89 | 88 | * @param defaultValue 默认值 |
90 | | - * @return 存在返回对应值,不存在返回默认值defaultValue |
| 89 | + * @return 存在返回对应值,不存在返回默认值{@code defaultValue} |
91 | 90 | */ |
92 | | - public static int getInt(Context context, String key, int defaultValue) { |
93 | | - return getSP(context).getInt(key, defaultValue); |
| 91 | + public int getInt(String key, int defaultValue) { |
| 92 | + return sp.getInt(key, defaultValue); |
94 | 93 | } |
95 | 94 |
|
96 | 95 | /** |
97 | 96 | * SP中写入long类型value |
98 | 97 | * |
99 | | - * @param context 上下文 |
100 | | - * @param key 键 |
101 | | - * @param value 值 |
102 | | - * @return {@code true}: 写入成功<br>{@code false}: 写入失败 |
| 98 | + * @param key 键 |
| 99 | + * @param value 值 |
103 | 100 | */ |
104 | | - public static boolean putLong(Context context, String key, long value) { |
105 | | - return getSP(context).edit().putLong(key, value).commit(); |
| 101 | + public void putLong(String key, long value) { |
| 102 | + editor.putLong(key, value).apply(); |
106 | 103 | } |
107 | 104 |
|
108 | 105 | /** |
109 | 106 | * SP中读取long |
110 | 107 | * |
111 | | - * @param context 上下文 |
112 | | - * @param key 键 |
| 108 | + * @param key 键 |
113 | 109 | * @return 存在返回对应值,不存在返回默认值-1 |
114 | 110 | */ |
115 | | - public static long getLong(Context context, String key) { |
116 | | - return getLong(context, key, -1); |
| 111 | + public long getLong(String key) { |
| 112 | + return getLong(key, -1L); |
117 | 113 | } |
118 | 114 |
|
119 | 115 | /** |
120 | 116 | * SP中读取long |
121 | 117 | * |
122 | | - * @param context 上下文 |
123 | 118 | * @param key 键 |
124 | 119 | * @param defaultValue 默认值 |
125 | | - * @return 存在返回对应值,不存在返回默认值defaultValue |
| 120 | + * @return 存在返回对应值,不存在返回默认值{@code defaultValue} |
126 | 121 | */ |
127 | | - public static long getLong(Context context, String key, long defaultValue) { |
128 | | - return getSP(context).getLong(key, defaultValue); |
| 122 | + public long getLong(String key, long defaultValue) { |
| 123 | + return sp.getLong(key, defaultValue); |
129 | 124 | } |
130 | 125 |
|
131 | 126 | /** |
132 | 127 | * SP中写入float类型value |
133 | 128 | * |
134 | | - * @param context 上下文 |
135 | | - * @param key 键 |
136 | | - * @param value 值 |
137 | | - * @return {@code true}: 写入成功<br>{@code false}: 写入失败 |
| 129 | + * @param key 键 |
| 130 | + * @param value 值 |
138 | 131 | */ |
139 | | - public static boolean putFloat(Context context, String key, float value) { |
140 | | - return getSP(context).edit().putFloat(key, value).commit(); |
| 132 | + public void putFloat(String key, float value) { |
| 133 | + editor.putFloat(key, value).apply(); |
141 | 134 | } |
142 | 135 |
|
143 | 136 | /** |
144 | 137 | * SP中读取float |
145 | 138 | * |
146 | | - * @param context 上下文 |
147 | | - * @param key 键 |
| 139 | + * @param key 键 |
148 | 140 | * @return 存在返回对应值,不存在返回默认值-1 |
149 | 141 | */ |
150 | | - public static float getFloat(Context context, String key) { |
151 | | - return getFloat(context, key, -1); |
| 142 | + public float getFloat(String key) { |
| 143 | + return getFloat(key, -1f); |
152 | 144 | } |
153 | 145 |
|
154 | 146 | /** |
155 | 147 | * SP中读取float |
156 | 148 | * |
157 | | - * @param context 上下文 |
158 | 149 | * @param key 键 |
159 | 150 | * @param defaultValue 默认值 |
160 | | - * @return 存在返回对应值,不存在返回默认值defaultValue |
| 151 | + * @return 存在返回对应值,不存在返回默认值{@code defaultValue} |
161 | 152 | */ |
162 | | - public static float getFloat(Context context, String key, float defaultValue) { |
163 | | - return getSP(context).getFloat(key, defaultValue); |
| 153 | + public float getFloat(String key, float defaultValue) { |
| 154 | + return sp.getFloat(key, defaultValue); |
164 | 155 | } |
165 | 156 |
|
166 | 157 | /** |
167 | 158 | * SP中写入boolean类型value |
168 | 159 | * |
169 | | - * @param context 上下文 |
170 | | - * @param key 键 |
171 | | - * @param value 值 |
172 | | - * @return {@code true}: 写入成功<br>{@code false}: 写入失败 |
| 160 | + * @param key 键 |
| 161 | + * @param value 值 |
173 | 162 | */ |
174 | | - public static boolean putBoolean(Context context, String key, boolean value) { |
175 | | - return getSP(context).edit().putBoolean(key, value).commit(); |
| 163 | + public void putBoolean(String key, boolean value) { |
| 164 | + editor.putBoolean(key, value).apply(); |
176 | 165 | } |
177 | 166 |
|
178 | 167 | /** |
179 | 168 | * SP中读取boolean |
180 | 169 | * |
181 | | - * @param context 上下文 |
182 | | - * @param key 键 |
183 | | - * @return 存在返回对应值,不存在返回默认值false |
| 170 | + * @param key 键 |
| 171 | + * @return 存在返回对应值,不存在返回默认值{@code false} |
184 | 172 | */ |
185 | | - public static boolean getBoolean(Context context, String key) { |
186 | | - return getBoolean(context, key, false); |
| 173 | + public boolean getBoolean(String key) { |
| 174 | + return getBoolean(key, false); |
187 | 175 | } |
188 | 176 |
|
189 | 177 | /** |
190 | 178 | * SP中读取boolean |
191 | 179 | * |
192 | | - * @param context 上下文 |
193 | 180 | * @param key 键 |
194 | 181 | * @param defaultValue 默认值 |
195 | | - * @return 存在返回对应值,不存在返回默认值defaultValue |
| 182 | + * @return 存在返回对应值,不存在返回默认值{@code defaultValue} |
196 | 183 | */ |
197 | | - public static boolean getBoolean(Context context, String key, boolean defaultValue) { |
198 | | - return getSP(context).getBoolean(key, defaultValue); |
| 184 | + public boolean getBoolean(String key, boolean defaultValue) { |
| 185 | + return sp.getBoolean(key, defaultValue); |
199 | 186 | } |
200 | 187 |
|
201 | 188 | /** |
202 | | - * 获取name为PREFERENCE_NAME的SP对象 |
| 189 | + * 获取sp中所有键值对 |
203 | 190 | * |
204 | | - * @param context 上下文 |
205 | | - * @return SP |
| 191 | + * @return Map对象 |
| 192 | + */ |
| 193 | + public Map<String, ?> getAll() { |
| 194 | + return sp.getAll(); |
| 195 | + } |
| 196 | + |
| 197 | + /** |
| 198 | + * 从sp中移除该key |
| 199 | + * |
| 200 | + * @param key 键 |
| 201 | + */ |
| 202 | + public void remove(String key) { |
| 203 | + editor.remove(key).apply(); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * 判断sp中是否存在该key |
| 208 | + * |
| 209 | + * @param key 键 |
| 210 | + * @return {@code true}: 存在<br>{@code false}: 不存在 |
| 211 | + */ |
| 212 | + public boolean contains(String key) { |
| 213 | + return sp.contains(key); |
| 214 | + } |
| 215 | + |
| 216 | + /** |
| 217 | + * 清除所有数据 |
206 | 218 | */ |
207 | | - private static SharedPreferences getSP(Context context) { |
208 | | - return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE); |
| 219 | + public void clear() { |
| 220 | + editor.clear().apply(); |
209 | 221 | } |
210 | 222 |
|
211 | 223 |
|
|
0 commit comments