0%

使用Sharepreference存储对象

使用SharePreference存储字符串什么的都很常见,但是存储对象也许有些同学会觉得很麻烦。其实很简单。下面直接看代码。

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
/**
* SharedPreferences工具类,可以保存object对象
* 存储时以object存储到本地,获取时返回的也是object对象,需要自己进行强制转换
* 也就是说,存的人和取的人要是同一个人才知道取出来的东西到底是个啥 ^_^
*/
public class SPUtil {

/**
* writeObject 方法负责写入特定类的对象的状态,以便相应的 readObject 方法可以还原它
* 最后,用Base64.encode将字节文件转换成Base64编码保存在String中
*
* @param object 待加密的转换为String的对象
* @return String 加密后的String
*/
private static String Object2String(Object object) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(object);
String string = new String(Base64.encode(byteArrayOutputStream.toByteArray(), Base64.DEFAULT));
objectOutputStream.close();
return string;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}

/**
* 使用Base64解密String,返回Object对象
*
* @param objectString 待解密的String
* @return object 解密后的object
*/
private static Object String2Object(String objectString) {
byte[] mobileBytes = Base64.decode(objectString.getBytes(), Base64.DEFAULT);
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(mobileBytes);
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object object = objectInputStream.readObject();
objectInputStream.close();
return object;
} catch (Exception e) {
e.printStackTrace();
return null;
}

}

/**
* 使用SharedPreference保存对象
*
* @param fileKey 储存文件的key
* @param key 储存对象的key
* @param saveObject 储存的对象
*/
public static void save(String fileKey, String key, Object saveObject, Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
String string = Object2String(saveObject);
editor.putString(key, string);
editor.commit();
}

/**
* 获取SharedPreference保存的对象
*
* @param fileKey 储存文件的key
* @param key 储存对象的key
* @return object 返回根据key得到的对象
*/
public static Object get(String fileKey, String key, Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
String string = sharedPreferences.getString(key, null);
if (string != null) {
Object object = String2Object(string);
return object;
} else {
return null;
}
}

/**
* 删除SharedPreference保存的对象
*
* @param fileKey 存储文件的key
* @param key 存储对象的key
*/
public static void delete(String fileKey, String key, Context context) {
SharedPreferences sharedPreferences = context.getSharedPreferences(fileKey, Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.clear();
editor.apply();
}
}