数据的存储与获取

SharedPreferences

SharedPreferences是Android平台上一个轻量级的存储类,用来保存应用程序的各种配置信息,其本质是以“键-值”对的方式保存数据的xml文件,其文件保存在/data/data//shared_prefs目录下。
在使用中,getSharedPreferences()方法接收两个参数,第一个参数用于指定SharedPreferences文件的名称,如果指定的文件不存在,则会创建;第二个参数用于指定操作模式。

操作模式

Context.MODE_PRIVATE:指定该SharedPreferences数据只能被本应用程序读、写;
Context.MODE_WORLD_READABLE: 指定该SharedPreferences数据能被其他应用程序读,但不能写;
Context.MODE_WORLD_WRITEABLE: 指定该SharedPreferences数据能被其他应用程序读、写;
Context.MODE_APPEND:该模式会检查文件是否存在,存在就往文件追加内容,否则就创建新文件;

注:Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE这两种模式已在 Android 4.2版本中被废弃了。

SharedPreferences数据的存储与获取

public class SPUtil {

    private static SPUtil instance;

    private SharedPreferences shared;
    private SharedPreferences.Editor editor;

    private static final String FILE_NAME = "sp_db";

    public static SPUtil getInstance(Context context) {
        if (instance == null) {
            synchronized (SPUtil.class) {
                if (instance == null) {
                    instance = new SPUtil(context);
                }
            }
        }
        return instance;
    }

    private SPUtil(Context context) {
        shared = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
        editor = shared.edit();
    }

    public void putString(String key, String value) {
        editor.putString(checkKey(key), value);
        editor.commit();
    }

    public String getString(String key, String defaultValue) {
        return shared.getString(key, defaultValue);
    }

    public void putBoolean(String key, boolean value) {
        editor.putBoolean(checkKey(key), value);
        editor.commit();
    }

    public boolean getBoolean(String key, boolean defaultValue) {
        return shared.getBoolean(key, defaultValue);
    }

    public void putInt(String key, int value) {
        editor.putInt(checkKey(key), value);
        editor.commit();
    }

    public int getInt(String key, int defaultValue) {
        return shared.getInt(key, defaultValue);
    }

    public void putLong(String key, long value) {
        editor.putLong(checkKey(key), value);
        editor.commit();
    }

    public long getLong(String key, long defaultValue) {
        return shared.getLong(key, defaultValue);
    }

    public void putFloat(String key, float value) {
        editor.putFloat(checkKey(key), value);
        editor.commit();
    }

    public float getFloat(String key, float defaultValue) {
        return shared.getFloat(key, defaultValue);
    }

    public boolean contains(String key) {
        return shared.contains(key);
    }

    public void remove(String key) {
        if (contains(key)) {
            editor.remove(key);
            editor.commit();
        }
    }

    public void clear() {
        editor.clear();
        editor.commit();
    }

    public String checkKey(String key) {
        if (TextUtils.isEmpty(key)) {
            return "SPUtil";
        } else {
            return key;
        }
    }

}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容