Android SharedPreferences工具类

一、代码

public class SpUtils {
    private static final String SP_NAME = "Config";

    // 声明SharedPreferences对象
    private SharedPreferences sp;
    // 声明SharedPreferences编辑器,用于用于修改SharedReferences对象中值的接口
    private SharedPreferences.Editor editor;
    // 声明单例
    private volatile static SpUtils mInstance = null;

    /**
     * 构造方法
     */
    private SpUtils() { }

    /**
     * 实现单例,使用双重锁
     * @return
     */
    public static SpUtils getInstance() {
        if (mInstance == null) {
            synchronized (SpUtils.class) {
                if (mInstance == null) {
                    mInstance = new SpUtils();
                }
            }
        }
        return mInstance;
    }

    /**
     * 初始化SharedPreferences
     * MODE_PRIVATE:只限于本应用读写
     * MODE_WORLD_READABLE:支持其他应用读,但是不能写
     * MODE_WORLD_WRITEABLE:其他应用可以写
     */
    @SuppressLint("CommitPrefEdits")
    public void initSp(Context mContext) {
        sp = mContext.getSharedPreferences(SP_NAME, Context.MODE_PRIVATE);
        editor = sp.edit();
    }

    /**
     * 写入int值
     * @param key
     * @param values
     */
    public void putInt(String key, int values) {
        editor.putInt(key, values);
        editor.commit();
    }

    /**
     * 获取int值
     * @param key
     * @param defValues
     * @return
     */
    public int getInt(String key, int defValues) {
        return sp.getInt(key, defValues);
    }

    /**
     * 写入String值
     * @param key
     * @param values
     */
    public void putString(String key, String values) {
        editor.putString(key, values);
        editor.commit();
    }

    /**
     * 获取String值
     * @param key
     * @param defValues
     * @return
     */
    public String getString(String key, String defValues) {
        return sp.getString(key, defValues);
    }

    /**
     * 写入Boolean值
     * @param key
     * @param values
     */
    public void putBoolean(String key, boolean values) {
        editor.putBoolean(key, values);
        editor.commit();
    }

    /**
     * 获取boolean值
     * @param key
     * @param defValues
     * @return
     */
    public boolean getBoolean(String key, boolean defValues) {
        return sp.getBoolean(key, defValues);
    }

    /**
     * 删除指定Key
     * @param key
     */
    public void deleteKey(String key) {
        editor.remove(key);
        editor.commit();
    }

}

二、注意事项

1、一定要在使用前initSp(),否则会报空指针异常。

2、实例化sp.edit()会报黄色警告:
SharedPreferences.edit() without a corresponding commit() or apply() call
,则需要添加@SuppressLint(“CommitPrefEdits”),警告便消除。

三、链接

1、SharedPreferences官方API:链接

2、SharedPreferences.Editor官方API:链接

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Android Hai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值