SharedUtil.java
4.07 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
package com.cnlive.goldenline.util;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;
import com.cnlive.goldenline.application.GoldenLineApplication;
import com.google.gson.Gson;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.StreamCorruptedException;
/**
* * SharedPreferences 的公具类
*
* @author teeker_bin
*/
public class SharedUtil
{
private static final String SP_NAME = "config";
private static SharedPreferences sharedPreferences = GoldenLineApplication.getInstance().getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
private static Editor editor = sharedPreferences.edit();
public static void removeData(String key)
{
editor.remove(key);
editor.apply();
}
public static String getString(String key, String defaultValue)
{
return sharedPreferences.getString(key, defaultValue);
}
public static int getInt(String key, int defaultValue)
{
return sharedPreferences.getInt(key, defaultValue);
}
public static boolean getBoolean(String key, boolean defaultValue)
{
return sharedPreferences.getBoolean(key, defaultValue);
}
public static float getFloat(String key, float defaultValue)
{
return sharedPreferences.getFloat(key, defaultValue);
}
public static void setString(String key, String value)
{
if (value == null)
{
removeData(key);
}
else
{
editor.putString(key, value);
editor.apply();
}
}
public static long getLong(String key, long defaultValue)
{
return sharedPreferences.getLong(key, defaultValue);
}
public static void setLong(String key, long value)
{
editor.putLong(key, value);
editor.apply();
}
public static void setInt(String key, int value)
{
editor.putInt(key, value);
editor.apply();
}
public static void setBoolean(String key, boolean value)
{
editor.putBoolean(key, value);
editor.apply();
}
public static void setFloat(String key, float value)
{
editor.putFloat(key, value);
editor.apply();
}
public static void clearData()
{
editor.clear();
editor.apply();
}
/**
* 将obj 序列化后以指定的键值 存储在sp
*
* @param obj
* @param key
* @throws IOException
*/
public static void serialize(Object obj, String key) throws IOException
{
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
String serStr = byteArrayOutputStream.toString("ISO-8859-1");
serStr = java.net.URLEncoder.encode(serStr, "UTF-8");
objectOutputStream.close();
byteArrayOutputStream.close();
editor.putString(key, serStr);
editor.putString(key + "json", new Gson().toJson(obj));
editor.apply();
}
/**
* 反序列化
*
* @param key
* @return
* @throws StreamCorruptedException
* @throws IOException
* @throws ClassNotFoundException
*/
public static Object deSerialize(String key) throws StreamCorruptedException, IOException, ClassNotFoundException
{
String string = getString(key, "");
if (TextUtils.isEmpty(string))
{
return null;
}
else
{
String redStr = java.net.URLDecoder.decode(string, "UTF-8");
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(redStr.getBytes("ISO-8859-1"));
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
Object object = objectInputStream.readObject();
objectInputStream.close();
byteArrayInputStream.close();
return object;
}
}
/**
* 保存序列化对象
*
* @param key
* @param t
*/
public static <T> void saveSerializeObject(String key, T t)
{
try
{
SharedUtil.serialize(t, key);
}
catch (IOException e)
{}
}
/**
* 取的序列化对象
*
* @param key
* @return
*/
public static Object getSerializeObject(String key)
{
try
{
return SharedUtil.deSerialize(key);
}
catch (Exception e)
{}
return null;
}
}