通過(guò)泛型簡(jiǎn)化SharedPreferences的儲(chǔ)存和獲取數(shù)據(jù)
關(guān)于Sharedferences的基本內(nèi)容
Sharedferences是一種輕型的數(shù)據(jù)存儲(chǔ)方式煎饼,是基于XML文件存儲(chǔ)key-value鍵值對(duì)數(shù)據(jù)。通常用來(lái)存儲(chǔ)一些簡(jiǎn)單的配置信息合溺,例如String看成、Int纯露、Long拓诸、Float和Boolean等簡(jiǎn)單數(shù)據(jù)類型蒙秒。
SharedPreferences是一個(gè)接口,不能直接創(chuàng)建SharedPreferences實(shí)例块请,可以通過(guò)Context提供的getSharedPreferences(String name, int mode)方法來(lái)獲取SharedPreferences實(shí)例。其中拳缠,name參數(shù)是要保存的xml文件的名字,mode參數(shù)可取的值有:
- Context.MODE_PRIVATE:指定SharedPreferences數(shù)據(jù)只能被本應(yīng)用程序讀墩新、寫(xiě),是默認(rèn)操作模式,在該模式下,寫(xiě)入的內(nèi)容會(huì)覆蓋原文件的內(nèi)容窟坐;
- Context.MODE_WORLD_READABLE:指定SharedPreferences可被其他應(yīng)用程序讀海渊,但不能寫(xiě);這個(gè)常量在API17中被棄用了哲鸳。
- Context.MODE_WORLD_WRITEABLE:指定SharedPreferences數(shù)據(jù)可被其他應(yīng)用程序讀臣疑、寫(xiě);;這個(gè)常量在API17中被棄用了徙菠。
- Context.MODE_APPEND:該模式會(huì)檢查文件是否存在讯沈,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件婿奔。
- 實(shí)現(xiàn)SharedPreferences存儲(chǔ)和獲取數(shù)據(jù)的步驟如下:
1)創(chuàng)建SharedPreferences對(duì)象缺狠;
2)創(chuàng)建SharedPreferences.Editor對(duì)象,用于存儲(chǔ)數(shù)據(jù)萍摊;
3)通過(guò)Editor對(duì)象的putXxx()方法挤茄,存儲(chǔ)key-value對(duì)數(shù)據(jù)信息;
4)通過(guò)Editor對(duì)象的commit()方法提交對(duì)SharedPreferences的修改;
5)通過(guò)SharedPreferences的getXxx()方法,獲取存儲(chǔ)的數(shù)據(jù);
- Activity還提供了另一個(gè)getPreferences(mode)方法操作SharedPreferences冰木,這個(gè)方法默認(rèn)使用當(dāng)前類不帶包名的類名作為文件的名稱穷劈。
public SharedPreferences getPreferences(int mode) {
return getSharedPreferences(getLocalClassName(), mode);
}
根據(jù)SharedPreferences的特點(diǎn),并結(jié)合java的泛型方法,使用下面的代碼可以簡(jiǎn)化代碼的書(shū)寫(xiě)
package com.cdc.glide;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
/**
* 通過(guò)java的泛型簡(jiǎn)化SharedPreferences的儲(chǔ)存和取用數(shù)據(jù)
* @author cdc
*
*/
public class SpUtil{
private Context context;
private String spName;
/**
*
* @param context 建議使用Application的上下文
* @param spName 保存數(shù)據(jù)的xml文件名:spName+".xml"
*/
public SpUtil(Context context,String spName){
this.context=context;
this.spName=spName;
}
private SharedPreferences getSP() {
return context.getSharedPreferences(spName, Context.MODE_PRIVATE);
}
public <T> void saveData(String key, T t) {
Editor editor = getSP().edit();
if (t instanceof String) {
editor.putString(key, (String) t).commit();
} else if (t instanceof Integer) {
editor.putInt(key, (Integer) t).commit();
} else if (t instanceof Boolean) {
editor.putBoolean(key, (Boolean) t).commit();
} else if (t instanceof Long) {
editor.putLong(key, (Long) t).commit();
} else if (t instanceof Float) {
editor.putFloat(key, (Float) t).commit();
}
}
@SuppressWarnings("unchecked")
public <T> T getData(String key, T t) {
SharedPreferences sp = getSP();
if (t instanceof String) {
String str = sp.getString(key, (String) t);
t = (T) str;
} else if (t instanceof Integer) {
Integer in = sp.getInt(key, (Integer) t);
t = (T) in;
} else if (t instanceof Long) {
Long lon = sp.getLong(key, (Long) t);
t = (T) lon;
} else if (t instanceof Float) {
Float fl = sp.getFloat(key, (Float) t);
t = (T) fl;
} else if (t instanceof Boolean) {
Boolean bl = sp.getBoolean(key, (Boolean) t);
t = (T) bl;
}
return t;
}
}