importandroid.app.Activity;importandroid.content.Context;importandroid.content.ContextWrapper;importandroid.content.SharedPreferences;importcom.imageviewpager.language.MyApplication;importjava.io.File;importjava.lang.reflect.Field;importjava.lang.reflect.InvocationTargetException;importjava.lang.reflect.Method;importjava.util.Map;public classSPUtil {/**debug 环境下允许修改 sp文件的路径*/
public static final boolean isDebug = true;/**修改以后的sp文件的路径 MyApplication.getContext().getExternalFilesDir(null).getAbsolutePath()=/sdcard/Android/%package_name%/file*/
public static final String FILE_PATH = MyApplication.getContext().getExternalFilesDir(null).getAbsolutePath();/*** 保存数据
*
*@paramcontext
*@paramfileName 文件名, 不需要".xml"
*@paramkeyName
*@paramvalue*/
public static voidput(Context context, String fileName, String keyName, Object value) {
SharedPreferences.Editor editor=getSharedPreferences(context, fileName).edit();if (value instanceofString) {
editor.putString(keyName, (String) value);
}else if (value instanceofInteger) {
editor.putInt(keyName, (Integer) value);
}else if (value instanceofBoolean) {
editor.putBoolean(keyName, (Boolean) value);
}else if (value instanceofFloat) {
editor.putFloat(keyName, (Float) value);
}else if (value instanceofLong) {
editor.putLong(keyName, (Long) value);
}else{
editor.putString(keyName, value.toString());
}
SharedPreferencesCompat.apply(editor);
}/*** 获取数据
*
*@paramcontext
*@paramfileName
*@paramkeyName
*@paramdefaultValue 默认值
*@return
*/
public staticObject get(Context context, String fileName, String keyName, Object defaultValue) {
SharedPreferences sp=getSharedPreferences(context, fileName);if (defaultValue instanceofString) {returnsp.getString(keyName, (String) defaultValue);
}else if (defaultValue instanceofInteger) {returnsp.getInt(keyName, (Integer) defaultValue);
}else if (defaultValue instanceofBoolean) {returnsp.getBoolean(keyName, (Boolean) defaultValue);
}else if (defaultValue instanceofFloat) {returnsp.getFloat(keyName, (Float) defaultValue);
}else if (defaultValue instanceofLong) {returnsp.getLong(keyName, (Long) defaultValue);
}return null;
}/*** 移除某个key值对应的值
*
*@paramcontext
*@paramfileName
*@paramkeyName*/
public static voidremove(Context context, String fileName, String keyName) {
SharedPreferences.Editor editor=getSharedPreferences(context, fileName).edit();
editor.remove(keyName);
SharedPreferencesCompat.apply(editor);
}/**清除所有数据*/
public static voidclear(Context context, String fileName) {
SharedPreferences.Editor editor=getSharedPreferences(context, fileName).edit();
editor.clear();
SharedPreferencesCompat.apply(editor);
}/*** 查询某个key是否已经存在
*
*@paramcontext
*@paramkeyName
*@return
*/
public static booleancontains(Context context, String fileName, String keyName) {returngetSharedPreferences(context, fileName).contains(keyName);
}/**返回所有的键值对*/
public static MapgetAll(Context context, String fileName) {returngetSharedPreferences(context, fileName).getAll();
}/**创建一个解决SharedPreferencesCompat.apply方法的一个兼容类*/
private static classSharedPreferencesCompat {private static final Method sApplyMethod =findApplyMethod();/**反射查找apply的方法*/@SuppressWarnings({"unchecked", "rawtypes"})private staticMethod findApplyMethod() {try{
Class clz= SharedPreferences.Editor.class;return clz.getMethod("apply");
}catch(NoSuchMethodException e) {
}return null;
}/**如果找到则使用apply执行,否则使用commit*/
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();
}
}/***@paramcontext
*@paramfileName
*@returnisDebug = 返回修改路径(路径不存在会自动创建)以后的 SharedPreferences :%FILE_PATH%/%fileName%.xml
* !isDebug = 返回默认路径下的 SharedPreferences : /data/data/%package_name%/shared_prefs/%fileName%.xml*/
private staticSharedPreferences getSharedPreferences(Context context, String fileName) {if(isDebug) {try{//获取ContextWrapper对象中的mBase变量。该变量保存了ContextImpl对象
Field field = ContextWrapper.class.getDeclaredField("mBase");
field.setAccessible(true);//获取mBase变量
Object obj =field.get(context);//获取ContextImpl。mPreferencesDir变量,该变量保存了数据文件的保存路径
field = obj.getClass().getDeclaredField("mPreferencesDir");
field.setAccessible(true);//创建自定义路径
File file = newFile(FILE_PATH);//修改mPreferencesDir变量的值
field.set(obj, file);//返回修改路径以后的 SharedPreferences :%FILE_PATH%/%fileName%.xml
returncontext.getSharedPreferences(fileName, Activity.MODE_PRIVATE);
}catch(NoSuchFieldException e) {
e.printStackTrace();
}catch(IllegalArgumentException e) {
e.printStackTrace();
}catch(IllegalAccessException e) {
e.printStackTrace();
}
}//返回默认路径下的 SharedPreferences : /data/data/%package_name%/shared_prefs/%fileName%.xml
returncontext.getSharedPreferences(fileName, Context.MODE_PRIVATE);
}
}