@@ -30,13 +30,7 @@ public class PreferencesManager {
30
30
31
31
private Context mContext ;
32
32
private SharedPreferences preferences ;
33
- private String DATA_URL = "/data/data/" ;
34
- private String SHARED_PREFS = "/shared_prefs" ;
35
-
36
33
private static String shareName = "SHARE_DATA" ;
37
- public static final String THEME = "Theme" ;
38
- public static final String LANG = "Lang" ;
39
-
40
34
private static PreferencesManager instance ;
41
35
42
36
/**
@@ -133,23 +127,6 @@ public void put(String key, long value) {
133
127
}
134
128
135
129
136
- /**
137
- * 直接存放对象,反射将根据对象的属性作为key,并将对应的值保存。
138
- *
139
- * @param t
140
- */
141
- @ SuppressWarnings ("rawtypes" )
142
- public <T > void put (T t ) {
143
- try {
144
- Editor edit = preferences .edit ();
145
- String json = JsonMananger .beanToJson (t );
146
- edit .putString (t .getClass ().getSimpleName (), json );
147
- edit .commit ();
148
- }catch (Exception e ) {
149
- e .printStackTrace ();
150
- }
151
- }
152
-
153
130
public String get (String key ) {
154
131
return preferences .getString (key , "" );
155
132
}
@@ -175,6 +152,23 @@ public long get(String key, long defValue) {
175
152
}
176
153
177
154
155
+ /**
156
+ * 直接存放对象,将对象转JSON,key为对象的classname
157
+ * @param t
158
+ */
159
+ @ SuppressWarnings ("rawtypes" )
160
+ public <T > void put (T t ) {
161
+ try {
162
+ Editor edit = preferences .edit ();
163
+ String json = JsonMananger .beanToJson (t );
164
+ edit .putString (t .getClass ().getSimpleName (), json );
165
+ edit .commit ();
166
+ }catch (Exception e ) {
167
+ e .printStackTrace ();
168
+ }
169
+ }
170
+
171
+
178
172
/**
179
173
* 获取整个对象,跟put(T t)对应使用
180
174
*
@@ -194,29 +188,13 @@ public <T> Object get(Class<T> cls) {
194
188
return obj ;
195
189
}
196
190
197
- public int getTheme (int defThemeId ) {
198
- return instance .get (THEME , defThemeId );
199
- }
200
-
201
- public void setTheme (int themeId ) {
202
- instance .put (THEME , themeId );
203
- }
204
-
205
- public String getLanguage (String defLang ) {
206
- return instance .get (LANG , defLang );
207
- }
208
-
209
- public void setLang (String Language ) {
210
- instance .put (LANG , Language );
211
- }
212
-
213
191
public void clearAll () {
214
192
try {
215
193
String fileName = shareName +".xml" ;
216
- StringBuilder path = new StringBuilder (DATA_URL ).append (mContext .getPackageName ()).append (SHARED_PREFS );
194
+ StringBuilder path = new StringBuilder ("/data/data/" ).append (mContext .getPackageName ()).append ("/shared_prefs" );
217
195
File file = new File (path .toString (), fileName );
218
196
if (file .exists ()) {
219
- file .delete ();
197
+ file .deleteOnExit ();
220
198
}
221
199
} catch (Exception e ) {
222
200
e .printStackTrace ();
0 commit comments