起因
sp使用時安卓中必不可少的,但是使用麻煩,幾乎每個人都會二次封裝,但是易用性就不見得了,話不多說直接上代碼(又進行了優化哦!!)
/** sp文件存取的封裝處理
* @Author feisher on 2017年10月26日09:50:08 更新,簡化代碼結構
* Email:[email protected]
*/
public class SPUtils {
//保存在手機裏面的文件名
public static final String FILE_NAME = "cache";
public static SharedPreferences sp ;
private static SharedPreferences init(Context context) {
return sp = context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE);
}
/**
* 存
* @param key 鍵
* @param value 值
* @param <E> 泛型,自動根據值進行處理
*/
public static <E>void put(@NonNull String key,@NonNull E value) {
put(MyApplication.getContext(),key,value);
}
/**
* 取
* @param key 鍵
* @param defaultValue 默認值
* @param <E> 泛型,自動根據值進行處理
* @return
*/
public static <E>E get(@NonNull String key,@NonNull E defaultValue) {
return get(MyApplication.getContext(),key,defaultValue);
}
/**
* 插件間和宿主共用數據 必須 傳入context
* @param context
* @param key
* @param value
* @return
*/
public static <E>void put(Context context,@NonNull String key,@NonNull E value) {
SharedPreferences.Editor editor = init(context).edit();
if (value instanceof String||value instanceof Integer||value instanceof Boolean||
value instanceof Float|| value instanceof Long||value instanceof Double) {
editor.putString(key, String.valueOf(value));
}else {
editor.putString(key, new Gson().toJson(value));
}
SPCompat.apply(editor);
}
/**
*插件間和宿主共用數據 必須 傳入context
* @param key
* @param defaultValue
* @return
*/
public static <E>E get(Context context,@NonNull String key,@NonNull E defaultValue) {
String value = init(context).getString(key, String.valueOf(defaultValue));
if (defaultValue instanceof String){
return (E) value;
}if (defaultValue instanceof Integer){
return (E) Integer.valueOf(value);
}if (defaultValue instanceof Boolean){
return (E) Boolean.valueOf(value);
}if (defaultValue instanceof Float){
return (E) Float.valueOf(value);
}if (defaultValue instanceof Long){
return (E) Long.valueOf(value);
}if (defaultValue instanceof Double){
return (E) Double.valueOf(value);
}
//json爲null的時候返回對象爲null,gson已處理
return (E) new Gson().fromJson(value,defaultValue.getClass());
}
/**
* 移除某個key值已經對應的值
* @param context
* @param key
*/
public static void remove(Context context, String key) {
SharedPreferences.Editor editor = init(context).edit();
editor.remove(key);
SPCompat.apply(editor);
}
/**
* 清除所有數據
* @param context
*/
public static void clear(Context context) {
SharedPreferences.Editor editor = init(context).edit();
editor.clear();
SPCompat.apply(editor);
}
/**
* 查詢某個key是否已經存在
*
* @param context
* @param key
* @return
*/
public static boolean contains(Context context, String key) {
return init(context).contains(key);
}
public static boolean contains(String key) {
return contains(MyApplication.getContext(),key);
}
/**
* 返回所有的鍵值對
*
* @param context
* @return
*/
public static Map<String, ?> getAll(Context context) {
return init(context).getAll();
}
/**
* 保存對象到sp文件中 被保存的對象須要實現 Serializable 接口
* @param key
* @param value
*/
public static void saveObject( String key, Object value) {
put(key,value);
}
/**
* desc:獲取保存的Object對象
* @param key
* @return modified:
*/
public static <T>T readObject(String key, Class<T> clazz) {
try {
return (T) get(key,clazz.newInstance());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 創建一個解決SharedPreferencesCompat.apply方法的一個兼容類
* @author
*/
private static class SPCompat {
private static final Method S_APPLY_METHOD = findApplyMethod();
/**
* 反射查找apply的方法
* @return
*/
@SuppressWarnings({"unchecked", "rawtypes"})
private static Method findApplyMethod() {
try {
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
} catch (NoSuchMethodException e) {
}
return null;
}
/**
* 如果找到則使用apply執行,否則使用commit
* @param editor
*/
public static void apply(SharedPreferences.Editor editor) {
try {
if (S_APPLY_METHOD != null) {
S_APPLY_METHOD.invoke(editor);
return;
}
} catch (Exception e) {
}
editor.commit();
}
}
}