Android SharedPreferences 最通用详细齐全工具类
网上有各种各样的SharedPreferences的工具类,无非就是那么几个,简单封装然后调用每一个封装方法,相当于每次新加一个要存储的key-value都需要去增加新的方法。
本工具,解决了需要一直增加方法的这个痛点问题。
- 新建接口IPreferences 为了统一调用初始化
import org.json.JSONArray;
import org.json.JSONObject;
public interface IPreferences {
/**
* Removes a key from the preferences.
* @param key The name of the preference to remove.
*/
void remove(final String key);
/**
* Judge if a key exist in the preferences.
* @param key The name of the preference to remove.
*/
boolean contains(String key);
/**
* Retrieve a boolean value from the preferences.
* @param key The name of the preference to retrieve.
* @param defaultValue Value to return if this preference does not exist.
* @return the preference value if it exists, or defaultValue.
*/
boolean getBoolean(final String key, final boolean defaultValue);
/**
* See getBoolean(final String key, final boolean defaultValue).
* @throws Exception If the key does not exist or the value is not a boolean.
*/
boolean getBoolean(final String key) throws Exception;
/**
* Saves a boolean value to the preferences.
* @param key The name of the preference to modify.
* @param value The new value for the preference.
*/
void putBoolean(final String key, final boolean value);
/**
* Retrieve an int value from the preferences.
* @param key The name of the preference to retrieve.
* @param defaultValue Value to return if this preference does not exist.
* @return the preference value if it exists, or defaultValue.
*/
int getInt(final String key, final int defaultValue);
/**
* See getInt(final String key, final int defaultValue).
* @throws Exception If the key does not exist or the value is not an integer.
*/
int getInt(final String key) throws Exception;
/**
* Saves an int value to the preferences.
* @param key The name of the preference to modify.
* @param value The new value for the preference.
*/
void putInt(final String key, final int value);
/**
* Retrieve a String value from the preferences.
* @param key The name of the preference to retrieve.
* @param defaultValue Value to return if this preference does not exist.
* @return the preference value if it exists, or defaultValue.
*/
String getString(final String key, final String defaultValue);
/**
* See getString(final String key, final String defaultValue).
* @throws Exception If the key does not exist or the value is not a String.
*/
String getString(final String key) throws Exception;
/**
* Saves a String value in the preferences.
* @param key The name of the preference to modify.
* @param value The new value for the preference.
*/
void putString(final String key, final String value);
/**
* Retrieve a JSONObject, from the preferences.
* @param key The name of the preference to retrieve.
* @param defaultValue Value to return if this preference does not exist.
* @return the preference value if it exists, or defaultValue.
*/
JSONObject getJSONObject(final String key, final JSONObject defaultValue);
/**
* See getJSONObject(final String key, final JSONObject defaultValue).
* @throws Exception If the key does not exist or the value is not a JSONObject.
*/
JSONObject getJSONObject(final String key) throws Exception;
/**
* Saves a JSONObject value in the preferences.
* @param key The name of the preference to modify.
* @param jsonObject The new value for the preference.
*/
void putJSONObject(final String key, final JSONObject jsonObject);
/**
* Retrieve a JSONArray, from the preferences.
* @param key The name of the preference to retrieve.
* @param defaultValue Value to return if this preference does not exist.
* @return the preference value if it exists, or defaultValue.
*/
JSONArray getJSONArray(final String key, final JSONArray defaultValue);
/**
* See getJSONArray(final String key, final JSONArray defaultValue).
* @throws Exception If the key does not exist or the value is not a JSONArray.
*/
JSONArray getJSONArray(final String key) throws Exception;
/**
* Saves a JSONArray value in the preferences.
* @param key The name of the preference to modify.
* @param jsonArray The new value for the preference.
*/
void putJSONArray(final String key, final JSONArray jsonArray);
}
- 新建Preferences 实现IPreferences 这个接口。
简单说明一下这个类。
(1)采用全局单例的模式。
(2)只需要初始化一次,至于初始化的方式,可以放在自己的baseActivity中,也可以放在Application中,强烈建议放在Application中,初始化一次即可。
(3)为什么会这样写,因为这样,我们只需要关心自己存储的数据类型和具体的key-value即可,不需要关心具体的实现。根据自己的需求,直接调用相应类型的方法。
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import org.json.JSONArray;
import org.json.JSONObject;
public class Preferences implements IPreferences {
private static final String TAG = "Preferences";
private SharedPreferences preferences;
@SuppressLint("StaticFieldLeak")
private static Preferences instance = null;
public static Preferences init(Context context) {
if (instance == null) {
synchronized (Preferences.class) {
if (instance == null) {
instance = new Preferences(context);
}
}
}
return instance;
}
private Preferences(Context context) {
if (preferences == null)
preferences = context.getSharedPreferences("myPreferences", Context.MODE_PRIVATE);
}
public static Preferences getPreferences() {
return instance;
}
private void throwIfKeyDoesNotExist(final String key) throws Exception {
if (!preferences.contains(key)) {
throw new Exception("Key " + key + " does not exist in preferences.");
}
}
@Override
public synchronized void remove(final String key) {
preferences.edit().remove(key).apply();
}
@Override
public synchronized boolean contains(String key) {
return preferences.contains(key);
}
@Override
public synchronized boolean getBoolean(final String key, final boolean defaultValue) {
boolean returnedValue = defaultValue;
try {
returnedValue = preferences.getBoolean(key, defaultValue);
} catch (Exception e) {
//TODO:这儿可以自定义自己的错误日志打印
}
return returnedValue;
}
@Override
public synchronized boolean getBoolean(final String key) throws Exception {
throwIfKeyDoesNotExist(key);
return getBoolean(key, false);
}
@Override
public synchronized void putBoolean(final String key, final boolean value) {
SharedPreferences.Editor preferencesEditor = preferences.edit();
preferencesEditor.putBoolean(key, value);
preferencesEditor.apply();
}
@Override
public synchronized int getInt(final String key, final int defaultValue) {
int returnedValue = defaultValue;
try {
returnedValue = preferences.getInt(key, defaultValue);
} catch (Exception e) {
//TODO:这儿可以自定义自己的错误日志打印
}
return returnedValue;
}
@Override
public synchronized int getInt(final String key) throws Exception {
throwIfKeyDoesNotExist(key);
return getInt(key, 0);
}
@Override
public synchronized void putInt(final String key, final int value) {
SharedPreferences.Editor preferencesEditor = preferences.edit();
preferencesEditor.putInt(key, value);
preferencesEditor.apply();
}
@Override
public synchronized String getString(final String key, final String defaultValue) {
String returnedValue = defaultValue;
try {
returnedValue = preferences.getString(key, defaultValue);
} catch (Exception e) {
//TODO:这儿可以自定义自己的错误日志打印
}
return returnedValue;
}
@Override
public synchronized String getString(final String key) throws Exception {
throwIfKeyDoesNotExist(key);
return getString(key, "");
}
@Override
public synchronized void putString(final String key, final String value) {
SharedPreferences.Editor preferencesEditor = preferences.edit();
preferencesEditor.putString(key, value);
preferencesEditor.apply();
}
@Override
public JSONObject getJSONObject(final String key, final JSONObject defaultValue) {
JSONObject returnedValue;
try {
String jsonString = preferences.getString(key, "");
returnedValue = (!jsonString.isEmpty() ? new JSONObject(jsonString) : null);
} catch (Exception e) {
returnedValue = defaultValue;
}
return returnedValue;
}
@Override
public synchronized JSONObject getJSONObject(final String key) throws Exception {
throwIfKeyDoesNotExist(key);
return getJSONObject(key, null);
}
@Override
public void putJSONObject(final String key, JSONObject jsonObject) {
putString(key, jsonObject.toString());
}
@Override
public JSONArray getJSONArray(final String key, final JSONArray defaultValue) {
JSONArray returnedValue;
try {
String jsonString = preferences.getString(key, "");
returnedValue = (!jsonString.isEmpty() ? new JSONArray(jsonString) : null);
} catch (Exception e) {
returnedValue = defaultValue;
}
return returnedValue;
}
@Override
public synchronized JSONArray getJSONArray(final String key) throws Exception {
throwIfKeyDoesNotExist(key);
return getJSONArray(key, null);
}
@Override
public void putJSONArray(final String key, final JSONArray jsonArray) {
putString(key, jsonArray.toString());
}
}
- 使用方式介绍
(1)初始化方式,在Application中,调用单例的初始化方式。注意引入Preferences的包路径。
import android.app.Application;
public class MyAppBase extends Application {
@Override
public void onCreate() {
super.onCreate();
Preferences.init(this);
}
}
(2)具体调用方式。
// 在具体需要调用的地方,其他类型的照旧即可。
// 存
Preferences.getPreferences().putInt("myKey", -1);
// 取
Preferences.getPreferences().getInt("myKey", -1)
- 最后就可以愉快的玩耍了,当然,初始化和调用的方式,可以根据自己的具体架构来具体的处理,每一个高明的架构,都会有一个很好的统一初始化和调用的接口的。