栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SharedPreferences

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SharedPreferences

SharedPreferences用于Android本地存储,使用较简单。SharedPreferences本身是一个接口,它的实现类是SharedPreferencesImpl,通过源码可以发现他的put和get方法都有synchronized锁,因此它是线程安全的。

SharedPreferences基本使用 存值
        SharedPreferences sp = context.getSharedPreferences("sp_storage", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putString("name", "小明");
        editor.apply();
//        editor.commit();

apply和commit的区别

editor.apply()会先存入内存,后续才同步到磁盘,比commit效率高,特别是频繁存取的时候。
editor.commit()会直接写入磁盘,并且返回boolean表示是否写入成功,适合需要根据是否成功写入磁盘这个结果做后续操作的情景使用。

取值
        SharedPreferences sp = context.getSharedPreferences("sp_storage", Context.MODE_PRIVATE);
        sp.getString("name", "");
SharedPreferences封装
//封装成Util
public class SPHelper {
    public static volatile SharedPreferences mSharedPreferences;

    public static void set(String key, int value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public static void set(String key, boolean value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public static void set(String key, String value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static void set(String key, long value) {
        SharedPreferences.Editor editor = getPreferences().edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public static boolean get(String key, boolean defValue) {
        return getPreferences().getBoolean(key, defValue);
    }

    public static String get(String key, String defValue) {
        return getPreferences().getString(key, defValue);
    }

    public static int get(String key, int defValue) {
        return getPreferences().getInt(key, defValue);
    }

    public static long get(String key, long defValue) {
        return getPreferences().getLong(key, defValue);
    }

    public static float get(String key, float defValue) {
        return getPreferences().getFloat(key, defValue);
    }

    public static SharedPreferences getPreferences() {
        if (mSharedPreferences == null) {
            synchronized (SPHelper.class) {
                if (mSharedPreferences == null) {
                    return baseApplication.getContext().getSharedPreferences("sp_storage", Context.MODE_PRIVATE);
                }
            }
        }
        return mSharedPreferences;
    }
}


//使用
//存
SPHelper.set("name", "小明");
//取
String name = SPHelper.get("name", "");

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/705277.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号