|
@@ -0,0 +1,157 @@
|
|
1
|
+package com.xhly.manageapp.utils;
|
|
2
|
+
|
|
3
|
+import android.content.Context;
|
|
4
|
+import android.content.SharedPreferences;
|
|
5
|
+import android.text.TextUtils;
|
|
6
|
+import android.util.Log;
|
|
7
|
+
|
|
8
|
+import com.google.gson.Gson;
|
|
9
|
+
|
|
10
|
+import java.lang.reflect.Type;
|
|
11
|
+
|
|
12
|
+public class CurrentAppSharedPreferencesUtils {
|
|
13
|
+ /**
|
|
14
|
+ * 保存在手机里面的文件名
|
|
15
|
+ */
|
|
16
|
+ public static final String SP_FILE_NAME = "ManageAppData";
|
|
17
|
+ private SharedPreferences preferences;
|
|
18
|
+ private static CurrentAppSharedPreferencesUtils instance;
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+ private CurrentAppSharedPreferencesUtils(Context context) {
|
|
22
|
+ preferences = context.getSharedPreferences(SP_FILE_NAME, Context.MODE_PRIVATE);
|
|
23
|
+ }
|
|
24
|
+
|
|
25
|
+ /**
|
|
26
|
+ * 单例模式的SharedPreferencesUtil
|
|
27
|
+ */
|
|
28
|
+ public static CurrentAppSharedPreferencesUtils getInstance(Context context) {
|
|
29
|
+ if (instance == null) {
|
|
30
|
+ synchronized (CurrentAppSharedPreferencesUtils.class) {
|
|
31
|
+ if (instance == null) {
|
|
32
|
+ instance = new CurrentAppSharedPreferencesUtils(context);
|
|
33
|
+ }
|
|
34
|
+ }
|
|
35
|
+ }
|
|
36
|
+ return instance;
|
|
37
|
+ }
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+ // *
|
|
41
|
+// * 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
|
|
42
|
+// * @param context
|
|
43
|
+// * @param key
|
|
44
|
+// * @param object
|
|
45
|
+//
|
|
46
|
+ public boolean setParam(String key, Object object) {
|
|
47
|
+
|
|
48
|
+ String type = object.getClass().getSimpleName();
|
|
49
|
+ SharedPreferences.Editor editor = preferences.edit();
|
|
50
|
+
|
|
51
|
+ if ("String".equals(type)) {
|
|
52
|
+ editor.putString(key, (String) object);
|
|
53
|
+ } else if ("Integer".equals(type)) {
|
|
54
|
+ editor.putInt(key, (Integer) object);
|
|
55
|
+ } else if ("Boolean".equals(type)) {
|
|
56
|
+ editor.putBoolean(key, (Boolean) object);
|
|
57
|
+ } else if ("Float".equals(type)) {
|
|
58
|
+ editor.putFloat(key, (Float) object);
|
|
59
|
+ } else if ("Long".equals(type)) {
|
|
60
|
+ editor.putLong(key, (Long) object);
|
|
61
|
+ }
|
|
62
|
+
|
|
63
|
+ editor.apply();
|
|
64
|
+ return false;
|
|
65
|
+ }
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+ // *
|
|
69
|
+// * 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
|
|
70
|
+// * @param context
|
|
71
|
+// * @param key
|
|
72
|
+// * @param defaultObject
|
|
73
|
+// * @return
|
|
74
|
+ public Object getParam(String key, Object defaultObject) {
|
|
75
|
+ String type = defaultObject.getClass().getSimpleName();
|
|
76
|
+ SharedPreferences sp = preferences;
|
|
77
|
+
|
|
78
|
+ if ("String".equals(type)) {
|
|
79
|
+ return sp.getString(key, (String) defaultObject);
|
|
80
|
+ } else if ("Integer".equals(type)) {
|
|
81
|
+ return sp.getInt(key, (Integer) defaultObject);
|
|
82
|
+ } else if ("Boolean".equals(type)) {
|
|
83
|
+ return sp.getBoolean(key, (Boolean) defaultObject);
|
|
84
|
+ } else if ("Float".equals(type)) {
|
|
85
|
+ return sp.getFloat(key, (Float) defaultObject);
|
|
86
|
+ } else if ("Long".equals(type)) {
|
|
87
|
+ return sp.getLong(key, (Long) defaultObject);
|
|
88
|
+ }
|
|
89
|
+
|
|
90
|
+ return null;
|
|
91
|
+ }
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+ /**
|
|
95
|
+ * 保存对象到json对象
|
|
96
|
+ *
|
|
97
|
+ * @param object 播放列表对象
|
|
98
|
+ */
|
|
99
|
+ public void saveJson(String key, Object object) {
|
|
100
|
+ String strJson = "";
|
|
101
|
+ Gson gson = new Gson();
|
|
102
|
+ strJson = gson.toJson(object);
|
|
103
|
+ SharedPreferences.Editor edit = preferences.edit();
|
|
104
|
+ if (edit != null) {
|
|
105
|
+ edit.putString(key, strJson);
|
|
106
|
+ edit.apply();
|
|
107
|
+ }
|
|
108
|
+ }
|
|
109
|
+
|
|
110
|
+ public void clearJson(String key) {
|
|
111
|
+ SharedPreferences.Editor edit = preferences.edit();
|
|
112
|
+ if (edit != null) {
|
|
113
|
+ edit.putString(key, "");
|
|
114
|
+ edit.apply();
|
|
115
|
+ }
|
|
116
|
+ }
|
|
117
|
+
|
|
118
|
+ /**
|
|
119
|
+ * 读取object对象
|
|
120
|
+ *
|
|
121
|
+ * @return
|
|
122
|
+ */
|
|
123
|
+ public <T> Object getFromJson(String key, Class<T> cls) {
|
|
124
|
+ try {
|
|
125
|
+ String json = preferences.getString(key, "");
|
|
126
|
+ if (TextUtils.isEmpty(json)) {
|
|
127
|
+ Log.d("转换失败", "数据为空");
|
|
128
|
+ return null;
|
|
129
|
+ }
|
|
130
|
+ Gson gson = new Gson();
|
|
131
|
+ return gson.fromJson(json, cls);
|
|
132
|
+ } catch (Exception e) {
|
|
133
|
+ Log.d("转换失败", e.toString());
|
|
134
|
+ return null;
|
|
135
|
+ }
|
|
136
|
+ }
|
|
137
|
+
|
|
138
|
+ public <T> Object getFromJsonType(String key, Class<T> cls, Type type) {
|
|
139
|
+ try {
|
|
140
|
+ String json = preferences.getString(key, "");
|
|
141
|
+ if (TextUtils.isEmpty(json)) {
|
|
142
|
+ Log.d("转换失败", "数据为空");
|
|
143
|
+ return null;
|
|
144
|
+ }
|
|
145
|
+ Gson gson = new Gson();
|
|
146
|
+ return gson.fromJson(json, type);
|
|
147
|
+ } catch (Exception e) {
|
|
148
|
+ Log.d("转换失败", e.toString());
|
|
149
|
+ return null;
|
|
150
|
+ }
|
|
151
|
+ }
|
|
152
|
+
|
|
153
|
+ public String get(String key) {
|
|
154
|
+ return preferences.getString(key, "");
|
|
155
|
+ }
|
|
156
|
+
|
|
157
|
+}
|