SharedPreferences 封装简单化使用

该博客介绍了如何创建一个名为SPTool的SharedPreferences封装类,简化Android中数据存储的操作。通过调用静态方法,可以方便地实现数据的保存、读取、删除和清空。在自定义的Application类中初始化SPTool,并提供了使用示例,例如在Constant类中获取和设置数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.创建SPTool类封装 SharedPreferences (只需把代码复制到项目即可)



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

import java.util.Set;

/**
 * @author Created by Hu
 * 版本:1.0
 * 创建日期:2020/9/24 19:19
 * 描述:
 */
public class SPTool {
    private static SharedPreferences sp;


    private SPTool() {
    }

    public static void init(Context context, String SpaceName) {
        sp = context.getSharedPreferences(SpaceName, 0);
    }

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


    public static void remove(String key) {
        sp.edit().remove(key).apply();
    }


    public static void clear() {
        sp.edit().clear().apply();
    }

    public static void put(String key, String value) {
        if (value == null) {
            sp.edit().remove(key).apply();
        } else {
            sp.edit().putString(key, value).apply();
        }
    }

    public static void put(String key, boolean value) {
        sp.edit().putBoolean(key, value).apply();
    }

    public static void put(String key, float value) {
        sp.edit().putFloat(key, value).apply();
    }

    public static void put(String key, long value) {
        sp.edit().putLong(key, value).apply();
    }

    public static void put(String key, int value) {
        sp.edit().putInt(key, value).apply();
    }

    public static void put(String key, String[] value) {
        SharedPreferences.Editor mEditor = sp.edit();
        mEditor.putInt(key + "_size", value.length);
        for (int i = 0; i < value.length; i++) {
            mEditor.remove(key + "_" + i);
            mEditor.putString(key + "_" + i, value[i]);
        }
        mEditor.apply();
    }

    public static void put(String key, Set<String> value) {
        sp.edit().putStringSet(key, value).apply();
    }

    public static String get(String key) {
        return sp.getString(key, "");
    }

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

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

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

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

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


    public static Set<String> get(String key, Set<String> defValue) {
        return sp.getStringSet(key, defValue);
    }

    public static String[] getArray(String key, String[] defValue) {
        int size = sp.getInt(key + "_size", 0);
        String[] value = new String[size];
        for (int i = 0; i < size; i++) {
            value[i] = sp.getString(key + "_" + i, null);
        }
        return value;
    }

}

2.继承Application 的类中进行初始化 

public class MyApp extends Application {
    public static Context context;
    public static Application app;

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        init();
    }

    /**
     * 初始化
     */
    private void init() {
      SPTool.init(context,"地址(自己定义,不可中文)");//初始化SPTool
    }

3.使用举例


/**
 * @author Created by Hu
 * 版本:1.0
 * 描述:
 */
public class Constant {

 public static String defaultValue = "defaultValue";//默认数据添加 开始的一次
  
    /**
     * 获取数据
     */
    public static String getValue() {
        return SPTool.get(defaultValue, "");

    }

    /**
     * 添加数据
     */
    public static void setValue(String value) {
        SPTool.put(defaultValue, value);
    }

   /**
     * 清除某一个数据
     */
    public static void removeValue() {
        SPTool.remove(defaultValue);
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值