Skip to content

Commit 37a223a

Browse files
committed
see 08/22 log
1 parent 94fe691 commit 37a223a

File tree

4 files changed

+206
-88
lines changed

4 files changed

+206
-88
lines changed

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
## Android开发人员不得不收集的代码(持续更新中)
1+
## Android开发人员不得不收集的代码([持续更新中][update_log.md])
22
***
33
为方便查找,已进行大致归类,其目录如下所示:
44
> - **App相关→[AppUtils.java][app.java]**
@@ -66,7 +66,6 @@
6666
> - AES解密 *decryptAES*
6767
6868
> - **文件相关→[FileUtils.java][file.java][Test][file.test]**
69-
> - 关闭IO *closeIO*
7069
> - 根据文件路径获取文件 *getFileByPath*
7170
> - 判断文件是否存在 *isFileExists*
7271
> - 判断是否是目录 *isDir*
@@ -88,6 +87,7 @@
8887
> - 指定编码按行读取文件到StringBuilder中 *readFile2SB*
8988
> - byte单位转换(单位:unit) *byte2Unit*
9089
> - 获取文件大小 *getFileSize*
90+
> - 关闭IO *closeIO*
9191
> - 根据全路径获取最长目录 *getDirName*
9292
> - 根据全路径获取文件名 *getFileName*
9393
> - 根据全路径获取文件名不带拓展名 *getFileNameNoExtension*
@@ -167,7 +167,8 @@
167167
> - 在onCreate()即可强行获取View的尺寸 *forceGetViewSize*
168168
> - ListView中提前测量View尺寸(注释萌萌哒) *measureView*
169169
170-
> - **SP相关→[SPUtils.java][sp.java]**
170+
> - **SP相关→[SPUtils.java][sp.java][Test][sp.test]**
171+
> - SPUtils构造函数 *SPUtils*
171172
> - SP中写入String类型value *putString*
172173
> - SP中读取String *getString*
173174
> - SP中写入int类型value *putInt*
@@ -178,6 +179,10 @@
178179
> - SP中读取float *getFloat*
179180
> - SP中写入boolean类型value *putBoolean*
180181
> - SP中读取boolean *getBoolean*
182+
> - 获取sp中所有键值对 *getAll*
183+
> - 从sp中移除该key *remove*
184+
> - 判断sp中是否存在该key *contains*
185+
> - 清除所有数据 *clear*
181186
182187
> - **字符串相关→[StringUtils.java][string.java][Test][string.test]**
183188
> - 判断字符串是否为null或长度为0 *isEmpty*
@@ -282,6 +287,7 @@ limitations under the License.
282287
[size.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/SizeUtils.java
283288

284289
[sp.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/SPUtils.java
290+
[sp.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/SPUtilsTest.java
285291

286292
[string.java]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/main/java/com/blankj/utilcode/utils/StringUtils.java
287293
[string.test]: https://github.com/Blankj/AndroidUtilCode/blob/master/utilcode/src/test/java/com/blankj/utilcode/utils/StringUtilsTest.java

update_log.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
### 更新Log
1+
###
2+
#### 16/08/22 SPUtils将commit改为apply提高效率,将SPUtils改为构造函数法创建,
23
#### 16/08/21 FileUtils单元测试完毕,修复FileUtils的bug,发布版本1.1.2
34
#### 16/08/20 更新目录,继续完善FileUtils单元测试,发布版本1.1.1
45
#### 16/08/19 继续完善FileUtils及单元测试,及其他小修小补(在此感谢vpop的三次Pr)

utilcode/src/main/java/com/blankj/utilcode/utils/SPUtils.java

Lines changed: 96 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -3,209 +3,221 @@
33
import android.content.Context;
44
import android.content.SharedPreferences;
55

6+
import java.util.Map;
7+
68
/**
79
* <pre>
810
* author: Blankj
911
* blog : http://blankj.com
1012
* time : 2016/8/2
11-
* desc : SP读写工具类
13+
* desc : SP相关工具类
1214
* </pre>
1315
*/
1416
public class SPUtils {
1517

16-
private SPUtils() {
17-
throw new UnsupportedOperationException("u can't fuck me...");
18-
}
18+
private SharedPreferences sp;
19+
private SharedPreferences.Editor editor;
1920

2021
/**
21-
* SP的name值
22-
* <p>可通过修改PREFERENCE_NAME变量修改SP的name值</p>
22+
* SPUtils构造函数
23+
*
24+
* @param context 上下文
25+
* @param spName spName
2326
*/
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+
}
2532

2633
/**
2734
* SP中写入String类型value
2835
*
29-
* @param context 上下文
30-
* @param key 键
31-
* @param value 值
32-
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
36+
* @param key 键
37+
* @param value 值
3338
*/
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();
3641
}
3742

3843
/**
3944
* SP中读取String
4045
*
41-
* @param context 上下文
42-
* @param key 键
43-
* @return 存在返回对应值,不存在返回默认值null
46+
* @param key 键
47+
* @return 存在返回对应值,不存在返回默认值{@code null}
4448
*/
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);
4751
}
4852

4953
/**
5054
* SP中读取String
5155
*
52-
* @param context 上下文
5356
* @param key 键
5457
* @param defaultValue 默认值
55-
* @return 存在返回对应值,不存在返回默认值defaultValue
58+
* @return 存在返回对应值,不存在返回默认值{@code defaultValue}
5659
*/
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);
5962
}
6063

6164
/**
6265
* SP中写入int类型value
6366
*
64-
* @param context 上下文
65-
* @param key 键
66-
* @param value 值
67-
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
67+
* @param key 键
68+
* @param value 值
6869
*/
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();
7172
}
7273

7374
/**
7475
* SP中读取int
7576
*
76-
* @param context 上下文
77-
* @param key 键
77+
* @param key 键
7878
* @return 存在返回对应值,不存在返回默认值-1
7979
*/
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);
8282
}
8383

8484
/**
8585
* SP中读取int
8686
*
87-
* @param context 上下文
8887
* @param key 键
8988
* @param defaultValue 默认值
90-
* @return 存在返回对应值,不存在返回默认值defaultValue
89+
* @return 存在返回对应值,不存在返回默认值{@code defaultValue}
9190
*/
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);
9493
}
9594

9695
/**
9796
* SP中写入long类型value
9897
*
99-
* @param context 上下文
100-
* @param key 键
101-
* @param value 值
102-
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
98+
* @param key 键
99+
* @param value 值
103100
*/
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();
106103
}
107104

108105
/**
109106
* SP中读取long
110107
*
111-
* @param context 上下文
112-
* @param key 键
108+
* @param key 键
113109
* @return 存在返回对应值,不存在返回默认值-1
114110
*/
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);
117113
}
118114

119115
/**
120116
* SP中读取long
121117
*
122-
* @param context 上下文
123118
* @param key 键
124119
* @param defaultValue 默认值
125-
* @return 存在返回对应值,不存在返回默认值defaultValue
120+
* @return 存在返回对应值,不存在返回默认值{@code defaultValue}
126121
*/
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);
129124
}
130125

131126
/**
132127
* SP中写入float类型value
133128
*
134-
* @param context 上下文
135-
* @param key 键
136-
* @param value 值
137-
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
129+
* @param key 键
130+
* @param value 值
138131
*/
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();
141134
}
142135

143136
/**
144137
* SP中读取float
145138
*
146-
* @param context 上下文
147-
* @param key 键
139+
* @param key 键
148140
* @return 存在返回对应值,不存在返回默认值-1
149141
*/
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);
152144
}
153145

154146
/**
155147
* SP中读取float
156148
*
157-
* @param context 上下文
158149
* @param key 键
159150
* @param defaultValue 默认值
160-
* @return 存在返回对应值,不存在返回默认值defaultValue
151+
* @return 存在返回对应值,不存在返回默认值{@code defaultValue}
161152
*/
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);
164155
}
165156

166157
/**
167158
* SP中写入boolean类型value
168159
*
169-
* @param context 上下文
170-
* @param key 键
171-
* @param value 值
172-
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
160+
* @param key 键
161+
* @param value 值
173162
*/
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();
176165
}
177166

178167
/**
179168
* SP中读取boolean
180169
*
181-
* @param context 上下文
182-
* @param key 键
183-
* @return 存在返回对应值,不存在返回默认值false
170+
* @param key 键
171+
* @return 存在返回对应值,不存在返回默认值{@code false}
184172
*/
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);
187175
}
188176

189177
/**
190178
* SP中读取boolean
191179
*
192-
* @param context 上下文
193180
* @param key 键
194181
* @param defaultValue 默认值
195-
* @return 存在返回对应值,不存在返回默认值defaultValue
182+
* @return 存在返回对应值,不存在返回默认值{@code defaultValue}
196183
*/
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);
199186
}
200187

201188
/**
202-
* 获取name为PREFERENCE_NAME的SP对象
189+
* 获取sp中所有键值对
203190
*
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+
* 清除所有数据
206218
*/
207-
private static SharedPreferences getSP(Context context) {
208-
return context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE);
219+
public void clear() {
220+
editor.clear().apply();
209221
}
210222

211223

0 commit comments

Comments
 (0)