SharedPreferences存储 单例模式
2016-08-27 15:41
274 查看
public class SharedUtils { private static SharedUtils sharedUtils; public static String SHARED_PREFS_NAME = Constants.APPNAME_ENGLISH; public static SharedUtils singleton() { if (sharedUtils == null) { sharedUtils = new SharedUtils(UIUtil.getContext(), SHARED_PREFS_NAME); } return sharedUtils; } private SharedPreferences mSharedPrefs; private SharedUtils(Context context, String sharedPrefsName) { mSharedPrefs = context.getSharedPreferences(sharedPrefsName, Context.MODE_PRIVATE); } public <T> void put(String key, T value) { put(new String[] { key }, new Object[] { value }); } public <T> void put(String[] keys, T[] values) { if (values != null && keys != null) { Editor edit = mSharedPrefs.edit(); for (int i = 0; i < values.length; i++) { T value = values[i]; int index = i; if (index >= keys.length) { index = keys.length - 1; } String key = keys[index]; Class<? extends Object> cls = value.getClass(); if (cls == Integer.class || cls == int.class) { edit.putInt(key, (Integer) value); } else if (cls == Boolean.class || cls == boolean.class) { edit.putBoolean(key, (Boolean) value); } else if (cls == Float.class || cls == float.class) { edit.putFloat(key, (Float) value); } else if (cls == Long.class || cls == long.class) { edit.putLong(key, (Long) value); } else if (cls == String.class) { edit.putString(key, (String) value); } } edit.commit(); } } @SuppressWarnings("unchecked") public <T> T get(String key, T defValue) { Object result = null; if (defValue != null && key != null) { Class<? extends Object> cls = defValue.getClass(); if (cls == Integer.class || cls == int.class) { result = mSharedPrefs.getInt(key, (Integer) defValue); } else if (cls == Boolean.class || cls == boolean.class) { result = mSharedPrefs.getBoolean(key, (Boolean) defValue); } else if (cls == Float.class || cls == float.class) { result = mSharedPrefs.getFloat(key, (Float) defValue); } else if (cls == Long.class || cls == long.class) { result = mSharedPrefs.getLong(key, (Long) defValue); } else if (cls == String.class) { result = mSharedPrefs.getString(key, (String) defValue); } } return (T) result; } public void clear() { Editor edit = mSharedPrefs.edit(); edit.clear(); edit.commit(); } public void clear(String key) { Editor edit = mSharedPrefs.edit(); edit.remove(key); edit.commit(); }}存储方式:SharedUtils sharedUtils = SharedUtils.singleton();
sharedUtils.put(String key, T value);
获取存储方式:
SharedUtils sharedUtils = SharedUtils.singleton();
sharedUtils.get(String key, T value);
相关文章推荐
- JS精确计算bug
- javascript创建对象
- javascript 函数初探 (六)--- 闭包初探#3
- json返回日期格式化的解决
- 前端知识点之CSS(二)
- 二两橙子的前端之路--简单算法的实现
- #85 Insert Node in a Binary Search Tree
- 从零开始学习jQuery (十一) 实战表单验证与自动完成提示插件
- 从零开始学习jQuery (十) jQueryUI常用功能实战
- 从零开始学习jQuery (九) jQuery工具函数
- 从零开始学习jQuery (八) 插播:jQuery实施方案
- HTML事件
- 从零开始学习jQuery (七) jQuery动画-让页面动起来!
- javascript 函数初探 (六)--- 闭包初探#2
- 从零开始学习jQuery (六) AJAX快餐
- 从零开始学习jQuery (五) 事件与事件对象
- 从零开始学习jQuery (四) 使用jQuery操作元素的属性与样式
- 宽屏banner切换jquery
- 从零开始学习jQuery (三) 管理jQuery包装集
- js select实现省市区联动选择