sharePreferences的简单封装

封装的代码

 

 

package com.liuan.ok_demo;

import android.content.Context;
import android.content.SharedPreferences;

import static com.example.liuan.mytoolsquickbottom.MyApp.appContext;

public class SpUtils {

    private static SharedPreferences sp;

    public static void getSharedPreference(Context context) {
        if (sp == null) {
            sp = context.getSharedPreferences("config", context.MODE_PRIVATE);
        }
    }

    public static void putString(Context context, String key, String value) {
        getSharedPreference(context);
        sp.edit().putString(key, value).commit();
    }

    private  static Context appContext;

    public static void init(Context context) {
        appContext = context;
    }

    public static String getString(Context context, String key, String defValue) {
        getSharedPreference(context);
        return sp.getString(key, defValue);
    }

    public static void putInt(Context context, String key, int value) {
        getSharedPreference(context);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(Context context, String key, int defValue) {
        getSharedPreference(context);
        return sp.getInt(key, defValue);
    }

    public static void putBoolean(Context context, String key, boolean value) {
        getSharedPreference(context);
        sp.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(Context context, String key,
                                     boolean defValue) {
        getSharedPreference(context);
        return sp.getBoolean(key, defValue);
    }

    public static void putLong(Context context, String key, Long value) {
        getSharedPreference(context);
        sp.edit().putLong(key, value).commit();
    }

    public static Long getLong(Context context, String key,
                               Long defValue) {
        getSharedPreference(context);
        return sp.getLong(key, defValue);
    }


    /**
     * 移除
     */
    public static void remove(Context context, String key) {
        getSharedPreference(context);
        sp.edit().remove(key).commit();
    }

    //单参数构造
    public static void putString(String key, String value) {
        getSharedPreference(appContext);
        sp.edit().putString(key, value).commit();
    }

    public static String getString(String key, String defValue) {
        getSharedPreference(appContext);
        return sp.getString(key, defValue);
    }

    public static void putInt(String key, int value) {
        getSharedPreference(appContext);
        sp.edit().putInt(key, value).commit();
    }

    public static int getInt(String key, int defValue) {
        getSharedPreference(appContext);
        return sp.getInt(key, defValue);
    }

    public static void putBoolean(String key, boolean value) {
        getSharedPreference(appContext);
        sp.edit().putBoolean(key, value).commit();
    }

    public static boolean getBoolean(String key,
                                     boolean defValue) {
        getSharedPreference(appContext);
        return sp.getBoolean(key, defValue);
    }

    public static void putLong(String key, Long value) {
        getSharedPreference(appContext);
        sp.edit().putLong(key, value).commit();
    }

    public static Long getLong(String key,
                               Long defValue) {
        getSharedPreference(appContext);
        return sp.getLong(key, defValue);
    }


    /**
     * 移除
     */
    public static void remove(String key) {
        getSharedPreference(appContext);
        sp.edit().remove(key).commit();
    }


}  


三个参数

Context.MODE_PRIVATE: 指定该SharedPreferences数据只能被本应用程序读、写。

Context.MODE_WORLD_READABLE:  指定该SharedPreferences数据能被其他应用程序读,但不能写。

Context.MODE_WORLD_WRITEABLE:  指定该SharedPreferences数据能被其他应用程序读,写

原理

底层是使用键值对的方式存储在xml中

数据存储在/data/data/<package name>/shared_prefs目录下

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

安果移不动

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

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

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

打赏作者

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

抵扣说明:

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

余额充值