安卓專案實戰之好用的SharedPreferences工具類封裝
阿新 • • 發佈:2018-12-07
SpUtils.java程式碼如下:
public class SpUtils {
/**
* 儲存在手機裡面的檔名,在這裡要特別注意,因為在Android中已經確定了SharedPreferences是以xml形式儲存,
* 所以,在填寫檔名引數時,不要給定“.xml”字尾,android會自動新增。只要直接寫上檔名即可
*/
// 未用到,本例建立SharedPreferences的方式為預設方式,預設配置檔名為preferences.xml,
// MODE_PRIVATE:表示該配置檔案只能被自己的應用程式訪問
public static final String FILE_NAME = "share_data";
private static SpUtils mInstance;
private SharedPreferences sp;
public static SpUtils getInstance() {
if (mInstance == null) {
synchronized (SpUtils.class) {
if (mInstance == null) {
mInstance = new SpUtils();
}
}
}
return mInstance;
}
private SpUtils() {
// SharedPreferences物件獲取方式2:每個應用都有一個預設的配置檔案preferences.xml,使用getDefaultSharedPreferences獲取。
sp = PreferenceManager.getDefaultSharedPreferences(MyApplication.context);
}
/**
* 儲存資料的方法,我們需要拿到儲存資料的具體型別,然後根據型別呼叫不同的儲存方法
*
* @param key
* @param object
*/
public void putValue(String key, Object object) {
// SharedPreferences物件獲取方式1:自定義配置檔名稱
// SharedPreferences sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
if (object instanceof String) {
editor.putString(key, (String) object);
} else if (object instanceof Integer) {
editor.putInt(key, (Integer) object);
} else if (object instanceof Boolean) {
editor.putBoolean(key, (Boolean) object);
} else if (object instanceof Float) {
editor.putFloat(key, (Float) object);
} else if (object instanceof Long) {
editor.putLong(key, (Long) object);
} else {
editor.putString(key, object.toString());
}
SharedPreferencesCompat.apply(editor);
}
/**
* 得到儲存資料的方法,我們根據預設值得到儲存的資料的具體型別,然後呼叫相對於的方法獲取值
*
* @param key
* @param defaultObject
* @return
*/
public Object getValue(String key, Object defaultObject) {
if (defaultObject instanceof String) {
return sp.getString(key, (String) defaultObject);
} else if (defaultObject instanceof Integer) {
return sp.getInt(key, (Integer) defaultObject);
} else if (defaultObject instanceof Boolean) {
return sp.getBoolean(key, (Boolean) defaultObject);
} else if (defaultObject instanceof Float) {
return sp.getFloat(key, (Float) defaultObject);
} else if (defaultObject instanceof Long) {
return sp.getLong(key, (Long) defaultObject);
}
return null;
}
/**
* 移除某個key值已經對應的值
*
* @param key
*/
public void remove(String key) {
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}
/**
* 清除所有資料
*/
public void clearAll() {
SharedPreferences.Editor editor = sp.edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}
/**
* 查詢某個key是否已經存在
*
* @param key
* @return
*/
public boolean contains(String key) {
return sp.contains(key);
}
/**
* 返回所有的鍵值對
*
* @return
*/
public Map<String, ?> getAll() {
return sp.getAll();
}
/**
* 建立一個解決SharedPreferencesCompat.apply方法的一個相容類
* 裡面所有的commit操作使用了SharedPreferencesCompat.apply進行了替代,目的是儘可能的使用apply代替commit
* 首先說下為什麼,因為commit方法是同步的,並且我們很多時候的commit操作都是UI執行緒中,畢竟是IO操作,儘可能非同步;
* 所以我們使用apply進行替代,apply非同步的進行寫入;
* 由於在apply是在API 9以後引入,所以有了這個相容;
*/
private static class SharedPreferencesCompat {
private static final Method sApplyMethod = findApplyMethod();
/**
* 反射查詢apply的方法
*
* @return
*/
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Method findApplyMethod() {
try {
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
} catch (NoSuchMethodException e) {
}
return null;
}
/**
* 如果找到則使用apply執行,否則使用commit
*
* @param editor
*/
public static void apply(SharedPreferences.Editor editor) {
try {
if (sApplyMethod != null) {
sApplyMethod.invoke(editor);
return;
}
} catch (IllegalArgumentException e) {
} catch (IllegalAccessException e) {
} catch (InvocationTargetException e) {
}
editor.commit();
}
}
}
其中在建立SharedPreference物件時使用了預設配置檔案的建立方式,為了避免記憶體洩漏等問題,Context物件傳入Application例項,MyApplication.java程式碼如下:
public class MyApplication extends Application {
public static Context context;
@Override
public void onCreate() {
super.onCreate();
context = this;
}
}
如何使用
取值:引數1:為要獲取的value值對應的key
引數2:必須指定,當引數1指定的key不存在時,預設返回的結果,此處的引數2必須給定值,並且和接收結果的資料型別保持一致,及時獲取String型別的值,引數2也必須指定,此處指定的目的主要是表名要獲取何種型別的值,通過檢視工具類的getValue方法我們也知道,返回結果的型別就是通過引數2的型別來動態判斷的。
String result = (String)SpUtils.getInstance().getValue("ignore","");
設值:引數1:key
引數2:value
該方法沒有返回值。
SpUtils.getInstance().putValue("ignore","2.0.1");