SharePreference的封装使用,让数据的本地缓存更简单

来源:互联网 发布:单片机应用技术实例 编辑:程序博客网 时间:2024/06/05 21:11
每一次调用SharePreference都需要些冗长的代码,来初始化护着获取对象。当前的工具类主要就是对SharePreference进行封装。通过简单的一条语句就可以进行数据的保存。
首先,我们需要对管理类在Appliaction之中进行初始化,GlobalVarible.init(context);
其次在工具类中声明每一个需要使用的变量。
以下方代码,我声明的变量Instance为例,保存和读取文件分别只需要下面两行代码即可。
GlobalVariable.INSTANCE.get();
GlobalVariable.INSTANCE.set("Hello World");
import android.content.Context;import android.content.SharedPreferences;import android.util.SparseArray;import com.google.gson.Gson;import com.videogo.constant.Constant;/** * 全局变量管理 */public class GlobalVariable<T> {    /**     * 定义区     */    // Ticket    public final static GlobalVariable<String> INSTANCE = new GlobalVariable<>(1, true, String.class, null);    /**     * 代码实现区     */    private static Gson mGson;    private static SparseArray<Object> values;    private static SharedPreferences sharedPreferences;    // 缓存到本地    private boolean cacheInLocal;    private Class<T> type;    private Object defaultValue;    private int index;    private Context mContext;    /**     * 定义     *     * @param index 索引(勿重复)     * @param cacheInLocal 是否保存到本地     * @param type 类型     * @param defaultValue 默认值     */    private GlobalVariable(int index, boolean cacheInLocal, Class<T> type, T defaultValue) {        this.cacheInLocal = cacheInLocal;        this.type = type;        this.defaultValue = defaultValue;        this.index = index;    }    public static void init(Context context) {        if (values == null){            values = new SparseArray<>();            mGson = new Gson();            sharedPreferences = context.getApplicationContext().getSharedPreferences(Constant.VIDEOGO_PREFERENCE_NAME, 0);        }    }    /**     * 取值     */    public T get() {        String key = getKey();        Object result = values.get(index);        if (result == null && cacheInLocal) {            if (type == String.class) {                result = sharedPreferences.getString(key, (String) defaultValue);            } else if (type == int.class || type == Integer.class) {                result = sharedPreferences.getInt(key, (int) defaultValue);            } else if (type == long.class || type == Long.class) {                result = sharedPreferences.getLong(key, (long) defaultValue);            } else if (type == boolean.class || type == Boolean.class) {                result = sharedPreferences.getBoolean(key, (boolean) defaultValue);            } else if (type == float.class || type == Float.class) {                result = sharedPreferences.getFloat(key, (float) defaultValue);            } else {                String json = sharedPreferences.getString(key, null);                result = json == null ? null : mGson.fromJson(json, type);            }        }        if (result == null) {            set((T) defaultValue);            return (T) defaultValue;        } else {            return (T) result;        }    }    /**     * 设值     *     * @param value 值     */    public void set(T value) {        String key = getKey();        if (value == null) {            values.remove(index);            if (cacheInLocal) {                sharedPreferences.edit().remove(key).apply();            }        } else {            if (value.getClass() != type) {                throw new IllegalArgumentException("value type is not correct");            }            values.put(index, value);            if (cacheInLocal) {                SharedPreferences.Editor editor = sharedPreferences.edit();                if (type == String.class) {                    editor.putString(key, (String) value);                } else if (type == int.class || type == Integer.class) {                    editor.putInt(key, (Integer) value);                } else if (type == long.class || type == Long.class) {                    editor.putLong(key, (Long) value);                } else if (type == boolean.class || type == Boolean.class) {                    editor.putBoolean(key, (Boolean) value);                } else if (type == float.class || type == Float.class) {                    editor.putFloat(key, (Float) value);                } else {                    editor.putString(key, mGson.toJson(value));                }                editor.apply();            }        }    }    /**     * 移除     */    public void remove() {        set(null);    }    private String getKey() {        return "SP_KEY_" + index;    }}


原创粉丝点击