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; }}
阅读全文
0 0
- SharePreference的封装使用,让数据的本地缓存更简单
- Android sharepreference 简单的使用
- 对Volley简单的封装,让网络请求更简单
- 对localStorage的封装,使用更简单
- AlertController工具类的封装,让AlertController变得更简单
- SharePreference的简单用法
- SharedPreferences的封装使用,让你的代码更优雅
- 工具类的封装-sharePreference的封装
- SharePreference的使用
- android sharePreference的使用
- SharePreference的使用
- Android SharePreference的使用
- SharePreference简单封装
- SharePreference封装使用
- 缓存让你的设计更专业
- 本地缓存的使用
- 关于sharePreference的数据Demo
- iOS 简单的本地缓存
- TCP 编程时的 flush() 问题
- IIS Express 启用目录浏览
- 分层开发
- bzoj1396 识别子串【解法一】
- SpringMVC 配置Jackson方法
- SharePreference的封装使用,让数据的本地缓存更简单
- [JavaEE] 关于JPA entity 重写equals()和hashcode()的说明
- Android之隐藏标题栏、状态栏
- 编程之美2.3 寻找发帖“水王”
- 洛谷 P2827 蚯蚓
- 数的读法
- 离散题目16--求自反闭包
- poj1741 Tree
- 离散题目17--求对称闭包