SPUtil.java
3.59 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
package com.chudiangameplay.android.tool;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.Gson;
import java.lang.reflect.Type;
/**
* Created by Administrator on 2018/8/14.
*/
public class SPUtil {
public final static String SHAREPREFERENCE_NAME = "gameplay";
/**
* 设置sp通用方法
*
* @param context
* @param key
* @param value
*/
public static void putValue(Context context, String key, Object value) {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
if (value instanceof String) {
edit.putString(key, (String) value);
} else if (value instanceof Integer) {
edit.putInt(key, (int) value);
} else if (value instanceof Boolean) {
edit.putBoolean(key, (boolean) value);
} else if (value instanceof Long){
edit.putLong(key, (Long) value);
}
edit.commit();
}
/**
* 获取sp通用方法
*
* @param context
* @param key
* @param defaultValue
*/
public static String getStringValue(Context context, String key, String defaultValue) {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
return sp.getString(key, defaultValue);
}
public static int getIntValue(Context context, String key, int defaultValue) {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
return sp.getInt(key, defaultValue);
}
public static long getLongValue(Context context, String key, int defaultValue) {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
return sp.getLong(key, defaultValue);
}
public static boolean getBoolValue(Context context, String key, boolean defaultValue) {
try {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
return sp.getBoolean(key, defaultValue);
} catch (Exception ex) {
return defaultValue;
}
}
/**
* 设置sp通用方法, 存json格式的object对象
*/
public static void putObjectValue(Context context, String key, Object value) {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor edit = sp.edit();
if (value != null) {
String json = new Gson().toJson(value);
edit.putString(key, json);
} else {
edit.putString(key, "");
}
edit.commit();
}
public static Object getObjectValue(Context context, Class<?> getClass, String key) {
Object obj = null;
try {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
String json = sp.getString(key, "");
obj = new Gson().fromJson(json, getClass);
return obj;
} catch (Exception ex) {
return obj;
}
}
public static Object getObjectValue(Context context, Type typeOfT, String key) {
Object obj = null;
try {
SharedPreferences sp = context.getSharedPreferences(SHAREPREFERENCE_NAME, Context.MODE_PRIVATE);
String json = sp.getString(key, "");
obj = new Gson().fromJson(json, typeOfT);
return obj;
} catch (Exception ex) {
return obj;
}
}
}