sharedPreferences工具类封装

来源:互联网 发布:棋牌辅助作弊软件 编辑:程序博客网 时间:2024/05/21 10:54

sharedPreferences是经常用到的安卓数据储存方式,相对sqlite更为轻量级。

关于sharedPreferences的用法简单介绍下:


以下是对数据的写入操作

SharedPreferences sp = getSharedPreferences("MyDB", Context.MODE_PRIVATE);SharedPreferences.Editor editor = sp.edit();//key:data   value:myDataeditor.putString("data","myData");editor.apply();

editor的apply()方法和commit()方法的区别

editor.commit()有返回值,成功返回 true ,失败则返回 false 。commit提交是同步的,apply方法为异步写入磁盘。所以如果在使用中不需要commit的返回值的话,从效率上推荐使用apply方法。

以下是对数据的读取操作

SharedPreferences sp = getSharedPreferences("MyDB", Context.MODE_PRIVATE);String getData = sp.getString("data","");


下面我对sharedPreferences做了一个封装,方便使用。此工具类包含getInstance()方法,使用单例模式创建实例,不用每次使用sharedPreferences都创建一个新的对象。

public class SharedPreferencesUtil {    private SharedPreferences sp;    private SharedPreferences.Editor editor;    private static SharedPreferencesUtil mInstance = null;    private static String SP_NAME = "My_db";    public SharedPreferencesUtil(Context context) {        setContext(context);    }    public void setContext(Context context){        if (context == null){            return;        }        sp = context.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);        editor = sp.edit();    }    public static SharedPreferencesUtil getInstance(Context context) {        if (mInstance == null) {            mInstance = new SharedPreferencesUtil(context);        }        return mInstance;    }    //获取key的value值    public String loadKey(String key) {        return sp.getString(key, "");    }    //保存key-value对    public void saveKey(String key, String value) {        editor.putString(key, value);        editor.apply();    }    //移除key-value对    public void removeKey(String key) {        editor.remove(key);        editor.apply();    }    //清除所有的key-value对    public void clearKeys() {        editor.clear();        editor.apply();    }    //获取boolean类型的value值    public boolean loadBooleanKey(String key) {        return sp.getBoolean(key, true);    }    //保存boolean类型的value值    public void saveBooleanKey(String key, boolean value) {        editor.putBoolean(key, value);        editor.apply();    }    //获取int类型的value值    public int loadIntKey(String key) {        return sp.getInt(key, 0);    }    //保存int类型的value值    public void saveIntKey(String key, int value) {        editor.putInt(key, value);        editor.apply();    }    //保存float类型的value值    public void saveFloatKey(String key, float value) {        editor.putFloat(key, value);        editor.apply();    }    //获取float类型的value值    public float loadFloatKey(String key) {        return sp.getFloat(key, 0);    }    //获取long类型的value值    public long loadLongKey(String key) {        return sp.getLong(key, 0);    }    //保存long类型的value值    public void saveLongKey(String key, long value) {        editor.putLong(key, value);        editor.apply();    }    //SharedPreferences实例是否包含key    public boolean isContains(String key) {        return sp.contains(key);    }}

引用此工具类的示例,需要传入一个context:

SharedPreferencesUtil sp = SharedPreferencesUtil.getInstance(getBaseContext());                sp.saveKey("key","value");//存储                sp.loadKey("key");//读取






1 0