SharedPreferencesUtil.java
8.32 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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package com.prws.common.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
/**
* Description: This's SharedPreferencesUtil
* Time: 2018/7/10 15:05
*
* @author lishiting
*/
public class SharedPreferencesUtil {
private static SharedPreferencesUtil util;
private static SharedPreferences sp;
private SharedPreferencesUtil(Context context, String name) {
sp = context.getSharedPreferences(name, Context.MODE_PRIVATE);
}
/**
* 初始化SharedPreferencesUtil,只需要初始化一次,建议在Application中初始化
*
* @param context 上下文对象
* @param name SharedPreferences Name
*/
public static void getInstance(Context context, String name) {
if (util == null) {
util = new SharedPreferencesUtil(context, name);
}
}
/**
* 保存数据到SharedPreferences
*
* @param key 键
* @param value 需要保存的数据
* @return 保存结果
*/
public static boolean putData(String key, Object value) {
boolean result;
SharedPreferences.Editor editor = sp.edit();
String type = value.getClass().getSimpleName();
try {
switch (type) {
case "Boolean":
editor.putBoolean(key, (Boolean) value);
break;
case "Long":
editor.putLong(key, (Long) value);
break;
case "Float":
editor.putFloat(key, (Float) value);
break;
case "String":
editor.putString(key, (String) value);
break;
case "Integer":
editor.putInt(key, (Integer) value);
break;
default:
Gson gson = new Gson();
String json = gson.toJson(value);
editor.putString(key, json);
break;
}
result = true;
} catch (Exception e) {
result = false;
e.printStackTrace();
}
editor.apply();
return result;
}
/**
* 获取SharedPreferences中保存的数据
*
* @param key 键
* @param defaultValue 获取失败默认值
* @return 从SharedPreferences读取的数据
*/
public static Object getData(String key, Object defaultValue) {
Object result;
String type = defaultValue.getClass().getSimpleName();
try {
switch (type) {
case "Boolean":
result = sp.getBoolean(key, (Boolean) defaultValue);
break;
case "Long":
result = sp.getLong(key, (Long) defaultValue);
break;
case "Float":
result = sp.getFloat(key, (Float) defaultValue);
break;
case "String":
result = sp.getString(key, (String) defaultValue);
break;
case "Integer":
result = sp.getInt(key, (Integer) defaultValue);
break;
default:
Gson gson = new Gson();
String json = sp.getString(key, "");
if (!json.equals("") && json.length() > 0) {
result = gson.fromJson(json, defaultValue.getClass());
} else {
result = defaultValue;
}
break;
}
} catch (Exception e) {
result = null;
e.printStackTrace();
}
return result;
}
/**
* 用于保存集合
*
* @param key key
* @param list 集合数据
* @return 保存结果
*/
public static <T> boolean putListData(String key, List<T> list) {
boolean result;
String type = list.get(0).getClass().getSimpleName();
SharedPreferences.Editor editor = sp.edit();
JsonArray array = new JsonArray();
try {
switch (type) {
case "Boolean":
for (int i = 0; i < list.size(); i++) {
array.add((Boolean) list.get(i));
}
break;
case "Long":
for (int i = 0; i < list.size(); i++) {
array.add((Long) list.get(i));
}
break;
case "Float":
for (int i = 0; i < list.size(); i++) {
array.add((Float) list.get(i));
}
break;
case "String":
for (int i = 0; i < list.size(); i++) {
array.add((String) list.get(i));
}
break;
case "Integer":
for (int i = 0; i < list.size(); i++) {
array.add((Integer) list.get(i));
}
break;
default:
Gson gson = new Gson();
for (int i = 0; i < list.size(); i++) {
JsonElement obj = gson.toJsonTree(list.get(i));
array.add(obj);
}
break;
}
editor.putString(key, array.toString());
result = true;
} catch (Exception e) {
result = false;
e.printStackTrace();
}
editor.apply();
return result;
}
/**
* 获取保存的List
*
* @param key key
* @return 对应的Lis集合
*/
public static <T> List<T> getListData(String key, Class<T> cls) {
List<T> list = new ArrayList<>();
String json = sp.getString(key, "");
if (!json.equals("") && json.length() > 0) {
Gson gson = new Gson();
JsonArray array = new JsonParser().parse(json).getAsJsonArray();
for (JsonElement elem : array) {
list.add(gson.fromJson(elem, cls));
}
}
return list;
}
/**
* 获删除数据
*
* @param key key
*/
public static void delData(String key) {
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
editor.apply();
}
/**
* 用于保存集合
*
* @param key key
* @param map map数据
* @return 保存结果
*/
public static <K, V> boolean putHashMapData(String key, Map<K, V> map) {
boolean result;
SharedPreferences.Editor editor = sp.edit();
try {
Gson gson = new Gson();
String json = gson.toJson(map);
editor.putString(key, json);
result = true;
} catch (Exception e) {
result = false;
e.printStackTrace();
}
editor.apply();
return result;
}
/**
* 用于保存集合
*
* @param key key
* @return HashMap
*/
public static <V> HashMap<String, V> getHashMapData(String key, Class<V> clsV) {
String json = sp.getString(key, "");
HashMap<String, V> map = new HashMap<>();
Gson gson = new Gson();
JsonObject obj = new JsonParser().parse(json).getAsJsonObject();
Set<Map.Entry<String, JsonElement>> entrySet = obj.entrySet();
for (Map.Entry<String, JsonElement> entry : entrySet) {
String entryKey = entry.getKey();
JsonObject value = (JsonObject) entry.getValue();
map.put(entryKey, gson.fromJson(value, clsV));
}
Log.e("SharedPreferencesUtil", obj.toString());
return map;
}
public static void clear(Context context) {
SharedPreferences preferences = context.getSharedPreferences("config", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.clear();
editor.commit();
}
}