Android 使用Singleton读写数据到SharedPreferences
示例
SharedPreferencesManager(Singleton)类用于读取和写入所有类型的数据。
import android.content.Context; import android.content.SharedPreferences; import android.util.Log; import com.google.gson.Gson; import java.lang.reflect.Type; /** * Singleton Class for accessing SharedPreferences, * should be initialized once in the beginning by any application component using static * method initialize(applicationContext) */ public class SharedPrefsManager { private static final String TAG = SharedPrefsManager.class.getName(); private SharedPreferences prefs; private static SharedPrefsManager uniqueInstance; public static final String PREF_NAME = "com.example.app"; private SharedPrefsManager(Context appContext) { prefs = appContext.getSharedPreferences(PREF_NAME, Context.MODE_PRIVATE); } /** * Throws IllegalStateException if this class is not initialized * * @return unique SharedPrefsManager instance */ public static SharedPrefsManager getInstance() { if (uniqueInstance == null) { throw new IllegalStateException( "SharedPrefsManager is not initialized, call initialize(applicationContext) " + "static method first"); } return uniqueInstance; } /** * Initialize this class using application Context, * should be called once in the beginning by any application Component * * @param appContext application context */ public static void initialize(Context appContext) { if (appContext == null) { throw new NullPointerException("Provided application context is null"); } if (uniqueInstance == null) { synchronized (SharedPrefsManager.class) { if (uniqueInstance == null) { uniqueInstance = new SharedPrefsManager(appContext); } } } } private SharedPreferences getPrefs() { return prefs; } /** * Clears all data in SharedPreferences */ public void clearPrefs() { SharedPreferences.Editoreditor = getPrefs().edit(); editor.clear(); editor.commit(); } public void removeKey(String key) { getPrefs().edit().remove(key).commit(); } public boolean containsKey(String key) { return getPrefs().contains(key); } public String getString(String key, String defValue) { return getPrefs().getString(key, defValue); } public String getString(String key) { return getString(key, null); } public void setString(String key, String value) { SharedPreferences.Editoreditor = getPrefs().edit(); editor.putString(key, value); editor.apply(); } public int getInt(String key, int defValue) { return getPrefs().getInt(key, defValue); } public int getInt(String key) { return getInt(key, 0); } public void setInt(String key, int value) { SharedPreferences.Editoreditor = getPrefs().edit(); editor.putInt(key, value); editor.apply(); } public long getLong(String key, long defValue) { return getPrefs().getLong(key, defValue); } public long getLong(String key) { return getLong(key, 0L); } public void setLong(String key, long value) { SharedPreferences.Editoreditor = getPrefs().edit(); editor.putLong(key, value); editor.apply(); } public boolean getBoolean(String key, boolean defValue) { return getPrefs().getBoolean(key, defValue); } public boolean getBoolean(String key) { return getBoolean(key, false); } public void setBoolean(String key, boolean value) { SharedPreferences.Editoreditor = getPrefs().edit(); editor.putBoolean(key, value); editor.apply(); } public boolean getFloat(String key) { return getFloat(key, 0f); } public boolean getFloat(String key, float defValue) { return getFloat(key, defValue); } public void setFloat(String key, Float value) { SharedPreferences.Editoreditor = getPrefs().edit(); editor.putFloat(key, value); editor.apply(); } /** * Persists an Object in prefs at the specified key, class of given Object must implement Model * interface * * @param key String * @param modelObject Object to persist * @param <M> Generic for Object */ public <M extends Model> void setObject(String key, M modelObject) { String value = createJSONStringFromObject(modelObject); SharedPreferences.Editoreditor = getPrefs().edit(); editor.putString(key, value); editor.apply(); } /** * Fetches the previously stored Object of given Class from prefs * * @param key String * @param classOfModelObject Class of persisted Object * @param <M> Generic for Object * @return Object of given class */ public <M extends Model> M getObject(String key, Class<M> classOfModelObject) { String jsonData = getPrefs().getString(key, null); if (null != jsonData) { try { Gson gson = new Gson(); M customObject = gson.fromJson(jsonData, classOfModelObject); return customObject; } catch (ClassCastException cce) { Log.d(TAG, "无法将从首选项获取的字符串转换为类型的集合 " + classOfModelObject.getName() + "\n" + cce.getMessage()); } } return null; } /** * Persists a Collection object in prefs at the specified key * * @param key String * @param dataCollection Collection Object * @param <C> Generic for Collection object */ public <C> void setCollection(String key, C dataCollection) { SharedPreferences.Editoreditor = getPrefs().edit(); String value = createJSONStringFromObject(dataCollection); editor.putString(key, value); editor.apply(); } /** * Fetches the previously stored Collection Object of given type from prefs * * @param key String * @param typeOfC Type of Collection Object * @param <C> Generic for Collection Object * @return Collection Object which can be casted */ public <C> C getCollection(String key, Type typeOfC) { String jsonData = getPrefs().getString(key, null); if (null != jsonData) { try { Gson gson = new Gson(); C arrFromPrefs = gson.fromJson(jsonData, typeOfC); return arrFromPrefs; } catch (ClassCastException cce) { Log.d(TAG, "无法将从首选项获取的字符串转换为类型的集合 " + typeOfC.toString() + "\n" + cce.getMessage()); } } return null; } public void registerPrefsListener(SharedPreferences.OnSharedPreferenceChangeListener listener) { getPrefs().registerOnSharedPreferenceChangeListener(listener); } public void unregisterPrefsListener( SharedPreferences.OnSharedPreferenceChangeListenerlistener) { getPrefs().unregisterOnSharedPreferenceChangeListener(listener); } publicSharedPreferences.EditorgetEditor() { return getPrefs().edit(); } private static String createJSONStringFromObject(Object object) { Gson gson = new Gson(); return gson.toJson(object); } }
Modelinterface这是通过类的实现Gson来避免混淆的。
public interface Model { }
Model接口的Proguard规则:
-keep interface com.example.app.Model -keep class * implements com.example.app.Model { *;}