- Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathSPUtils.java
More file actions
Latest commit
102 lines (90 loc) · 3.09 KB
/
Copy pathSPUtils.java
File metadata and controls
102 lines (90 loc) · 3.09 KB
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
packagecom.hht.baselib;
importandroid.content.Context;
importandroid.content.SharedPreferences;
importjava.util.Map;
/**
* Created by RealMo on 2019/6/25
*/
publicclassSPUtils {
/**
* 保存数据
*/
publicstaticvoidput(Contextcontext, StringfileName,Stringkey, Objectobj,booleanasync) {
SharedPreferencessp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
SharedPreferences.Editoreditor = sp.edit();
if (objinstanceofBoolean) {
editor.putBoolean(key, (Boolean) obj);
} elseif (objinstanceofFloat) {
editor.putFloat(key, (Float) obj);
} elseif (objinstanceofInteger) {
editor.putInt(key, (Integer) obj);
} elseif (objinstanceofLong) {
editor.putLong(key, (Long) obj);
} else {
editor.putString(key, (String) obj);
}
if(async){
editor.apply();
}else{
editor.commit();
}
}
/**
* 获取指定数据
*/
publicstaticObjectget(Contextcontext, StringfileName,Stringkey, ObjectdefaultObj) {
SharedPreferencessp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
if (defaultObjinstanceofBoolean) {
returnsp.getBoolean(key, (Boolean) defaultObj);
} elseif (defaultObjinstanceofFloat) {
returnsp.getFloat(key, (Float) defaultObj);
} elseif (defaultObjinstanceofInteger) {
returnsp.getInt(key, (Integer) defaultObj);
} elseif (defaultObjinstanceofLong) {
returnsp.getLong(key, (Long) defaultObj);
} elseif (defaultObjinstanceofString) {
returnsp.getString(key, (String) defaultObj);
}
returnnull;
}
/**
* 删除指定数据
*/
publicstaticvoidremove(Contextcontext, StringfileName,Stringkey,booleanasync) {
SharedPreferencessp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
SharedPreferences.Editoreditor = sp.edit();
editor.remove(key);
if(async){
editor.apply();
}else{
editor.commit();
}
}
/**
* 返回所有键值对
*/
publicstaticMap<String, ?> getAll(Contextcontext,StringfileName) {
SharedPreferencessp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
returnsp.getAll();
}
/**
* 删除所有数据
*/
publicstaticvoidclear(Contextcontext,StringfileName,booleanasync) {
SharedPreferencessp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
SharedPreferences.Editoreditor = sp.edit();
editor.clear();
if(async){
editor.apply();
}else{
editor.commit();
}
}
/**
* 检查key对应的数据是否存在
*/
publicstaticbooleancontains(Contextcontext,StringfileName, Stringkey) {
SharedPreferencessp = context.getSharedPreferences(fileName, Context.MODE_PRIVATE);
returnsp.contains(key);
}
}