packagecom.example.myapplication;importandroid.content.Context;importandroid.content.SharedPreferences;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.util.Map;/*** Stay hungry , Stay foolish
* Copyright(c) 2016 www.wuxianedu.com Inc. All rights reserved.*/
public classSharedPreferenceUtil {public static final String FILE_NAME = "share_data"; //SharedPreference操作的文件
/*** 保存数据的方法,我们需要拿到保存数据的具体类型,然后根据类型调用不同的保存方法
*@paramcontext
*@paramkey
*@paramobject*/
public static voidput(Context context, String key, Object object) {
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();if (object instanceofString) {
editor.putString(key, (String) object);
}else if (object instanceofInteger) {
editor.putInt(key, (Integer) object);
}else if (object instanceofBoolean) {
editor.putBoolean(key, (Boolean) object);
}else if (object instanceofFloat) {
editor.putFloat(key, (Float) object);
}else if (object instanceofLong) {
editor.putLong(key, (Long) object);
}else{
editor.putString(key, object.toString());
}
SharedPreferencesCompat.apply(editor);
}/*** 得到保存数据的方法,我们根据默认值得到保存的数据的具体类型,然后调用相对于的方法获取值
*
*@paramcontext
*@paramkey
*@paramdefaultObject
*@return
*/
public staticObject get(Context context, String key, Object defaultObject) {
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);if (defaultObject instanceof String || defaultObject==null) {returnsp.getString(key, (String) defaultObject);
}else if (defaultObject instanceofInteger) {returnsp.getInt(key, (Integer) defaultObject);
}else if (defaultObject instanceofBoolean) {returnsp.getBoolean(key, (Boolean) defaultObject);
}else if (defaultObject instanceofFloat) {returnsp.getFloat(key, (Float) defaultObject);
}else if (defaultObject instanceofLong) {returnsp.getLong(key, (Long) defaultObject);
}return null;
}/*** 移除某个key值已经对应的值
*
*@paramcontext
*@paramkey*/
public static voidremove(Context context, String key) {
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.remove(key);
SharedPreferencesCompat.apply(editor);
}/*** 返回所有的键值对
*
*@paramcontext
*@return
*/
public static MapgetAll(Context context) {
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);returnsp.getAll();
}/*** 清除所有数据
*
*@paramcontext*/
public static voidclearAll(Context context) {
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}/*** 查询某个key是否已经存在
*
*@paramcontext
*@paramkey
*@return
*/
public static booleancontains(Context context, String key) {
SharedPreferences sp=context.getSharedPreferences(FILE_NAME,
Context.MODE_PRIVATE);returnsp.contains(key);
}/*** 创建一个解决SharedPreferencesCompat.apply方法的一个兼容类*/
private static classSharedPreferencesCompat {private static final Method sApplyMethod =findApplyMethod();/*** 反射查找apply的方法
*@return
*/@SuppressWarnings({"unchecked", "rawtypes"})private staticMethod findApplyMethod() {try{
Class clz= SharedPreferences.Editor.class;return clz.getMethod("apply");
}catch(NoSuchMethodException e) {
}return null;
}/*** 如果找到则使用apply执行,否则使用commit
*
* 里面所有的commit操作使用了SharedPreferencesCompat.apply进行了替代,目的是尽可能的使用apply代替commit
* 首先说下为什么,因为commit方法是同步的,并且我们很多时候的commit操作都是UI线程中,毕竟是IO操作,尽可能异步;
* 所以我们使用apply进行替代,apply异步的进行写入;
*@parameditor*/
public static voidapply(SharedPreferences.Editor editor) {try{if (sApplyMethod != null) {
sApplyMethod.invoke(editor);return;
}
}catch(IllegalArgumentException e) {
}catch(IllegalAccessException e) {
}catch(InvocationTargetException e) {
}
editor.commit();
}
}
}