Fork me on GitHub

SharedPreference工具类

SharedPreference工具类

代码

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
public class SharedPreferenceUtil {

public static void putSharedPreferences(String key, Object value) {
Context context = AppManager.getInstance().getAppContext();
String type = value.getClass().getSimpleName();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = sharedPreferences.edit();
if ("Integer".equals(type)) {
editor.putInt(key, (Integer) value);
} else if ("Boolean".equals(type)) {
editor.putBoolean(key, (Boolean) value);
} else if ("String".equals(type)) {
editor.putString(key, (String) value);
} else if ("Float".equals(type)) {
editor.putFloat(key, (Float) value);
} else if ("Long".equals(type)) {
editor.putLong(key, (Long) value);
}
editor.commit();
}

public static Object getSharedPreferences(String key, Object defValue) {
Context context = AppManager.getInstance().getAppContext();

String type = defValue.getClass().getSimpleName();
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
//defValue为为默认值,如果当前获取不到数据就返回它
if ("Integer".equals(type)) {
return sharedPreferences.getInt(key, (Integer) defValue);
} else if ("Boolean".equals(type)) {
return sharedPreferences.getBoolean(key, (Boolean) defValue);
} else if ("String".equals(type)) {
return sharedPreferences.getString(key, (String) defValue);
} else if ("Float".equals(type)) {
return sharedPreferences.getFloat(key, (Float) defValue);
} else if ("Long".equals(type)) {
return sharedPreferences.getLong(key, (Long) defValue);
}
return null;
}
}