SharedPrefrenceUtils.java
7.5 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
package com.hh.xuetubao.Utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Base64;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2016/6/13.
*/
public class SharedPrefrenceUtils {
private static SharedPreferences sp;
public static void saveBoolean(Context context, String key, boolean value) {
if (sp == null) {
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
sp.edit().putBoolean(key, value).commit();
}
public static boolean getBoolean(Context context, String key, boolean defValue) {
if (sp == null) {
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
return sp.getBoolean(key, defValue);
}
public static boolean getBoolean(Context context, String key) {
if (sp == null) {
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
return sp.getBoolean(key, false);
}
public static void saveString(Context context, String key, String value) {
if (sp == null) {
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
sp.edit().putString(key, value).commit();
}
public static String getString(Context context, String key, String defValue) {
if (sp == null) {
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
return sp.getString(key, defValue);
}
public static String getString(Context context, String key) {
if (sp == null) {
sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
}
return sp.getString(key, "");
}
/**
* 保存对象
*
* @param context 上下文
* @param key 键
* @param obj 要保存的对象(Serializable的子类)
* @param <T> 泛型定义
*/
public static <T extends Serializable> void putObject(Context context, String key, T obj) {
try {
put(context, key, obj);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取对象
*
* @param context 上下文
* @param key 键
* @param <T> 指定泛型
* @return 泛型对象
*/
public static <T extends Serializable> T getObject(Context context, String key) {
try {
return (T) get(context, key);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static void setFloat(Context context, final String key,
final float value) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
settings.edit().putFloat(key, value).apply();
}
public static float getFloat(Context context, final String key,
final float defaultValue) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getFloat(key, defaultValue);
}
public static void setLong(Context context, final String key,
final long value) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
settings.edit().putLong(key, value).apply();
}
public static long getLong(Context context, final String key,
final long defaultValue) {
final SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(context);
return settings.getLong(key, defaultValue);
}
public static void putStringList(Context context, String key, List<String> list) {
try {
put(context, key, list);
} catch (Exception e) {
e.printStackTrace();
}
}
public static List<String> getStringList(Context context, String key) {
try {
return (List<String>) get(context, key);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 存储List集合
*
* @param context 上下文
* @param key 存储的键
* @param list 存储的集合
*/
public static void putSerializableList(Context context, String key, List<? extends Serializable> list) {
try {
put(context, key, list);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 获取List集合
*
* @param context 上下文
* @param key 键
* @param <E> 指定泛型
* @return List集合
*/
public static <E extends Serializable> List<E> getSerializableList(Context context, String key) {
try {
return (List<E>) get(context, key);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 存储Map集合
*
* @param context 上下文
* @param key 键
* @param map 存储的集合
* @param <K> 指定Map的键
* @param <V> 指定Map的值
*/
public static <K extends Serializable, V extends Serializable> void putMap(Context context,
String key, Map<K, V> map) {
try {
put(context, key, map);
} catch (Exception e) {
e.printStackTrace();
}
}
public static <K extends Serializable, V extends Serializable> Map<K, V> getMap(Context context, String key) {
try {
return (Map<K, V>) get(context, key);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 存储对象
*/
private static void put(Context context, String key, Object obj)
throws IOException {
if (obj == null) {//判断对象是否为空
return;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ObjectOutputStream oos = null;
oos = new ObjectOutputStream(baos);
oos.writeObject(obj);
// 将对象放到OutputStream中
// 将对象转换成byte数组,并将其进行base64编码
String objectStr = new String(Base64.encode(baos.toByteArray(), Base64.DEFAULT));
baos.close();
oos.close();
saveString(context, key, objectStr);
}
/**
* 获取对象
*/
private static Object get(Context context, String key)
throws IOException, ClassNotFoundException {
String wordBase64 = getString(context, key);
// 将base64格式字符串还原成byte数组
if (TextUtils.isEmpty(wordBase64)) { //不可少,否则在下面会报java.io.StreamCorruptedException
return null;
}
byte[] objBytes = Base64.decode(wordBase64.getBytes(), Base64.DEFAULT);
ByteArrayInputStream bais = new ByteArrayInputStream(objBytes);
ObjectInputStream ois = new ObjectInputStream(bais);
// 将byte数组转换成product对象
Object obj = ois.readObject();
bais.close();
ois.close();
return obj;
}
}