SharedPreferences 封装简单化使用

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 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 get(String key, Set 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);
    }
}


 

你可能感兴趣的:(存储工具,Android,android,android,studio)