模板如下:
/**
* Created by SerryWang
* on 2018/8/20
*/
public class SharePref {
public static final String FILE_NAME = "share_data";
/**
*
* @param context
* @param key
* @param object
*/
public static void put(Context context, String key, Object object){
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
Editor edit = sp.edit();
if(object instanceof String){
edit.putString(key,(String)object);
}else if(object instanceof Integer){
edit.putInt(key,(Integer)object);
}else if(object instanceof Boolean){
edit.putBoolean(key,(Boolean)object);
}else if(object instanceof Float){
edit.putFloat(key,(Float)object);
}else if(object instanceof Long){
edit.putLong(key,(Long)object);
}else{
edit.putString(key,object.toString());
}
edit.apply();
}
/**
* 根據(jù)默認(rèn)值得到類(lèi)型
* @param context
* @param key
* @param defaultObject
*/
public static Object get(Context context,String key,Object defaultObject){
SharedPreferences sharedPreferences = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
if(defaultObject instanceof String){
return sharedPreferences.getString(key,(String) defaultObject);
}else if(defaultObject instanceof Integer){
return sharedPreferences.getInt(key,(Integer)defaultObject);
}else if(defaultObject instanceof Boolean){
return sharedPreferences.getBoolean(key,(Boolean)defaultObject);
}else if(defaultObject instanceof Float){
return sharedPreferences.getFloat(key,(Float)defaultObject);
}else if(defaultObject instanceof Long){
return sharedPreferences.getLong(key,(Long)defaultObject);
}
return null;
}
/**
* 移除某個(gè)數(shù)據(jù)
* @param context
* @param key
*/
public static void remove(Context context,String key){
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.remove(key);
editor.apply();
}
/**
* 清除所有數(shù)據(jù)
* @param context
*/
public static void clear(Context context){
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.clear();
editor.apply();
}
/**
* 查詢某條記錄是否存在
* @param context
* @param key
* @return
*/
public static boolean icContains(Context context,String key){
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
return sp.contains(key);
}
/**
* 返回所有的鍵值對(duì)
* @param context
* @return
*/
public static Map<String,?>getAll(Context context){
SharedPreferences sp = context.getSharedPreferences(FILE_NAME,Context.MODE_PRIVATE);
return sp.getAll();
}
private static class SharePrefencesCompat{
private static Method sApplyMethod = null;
static {
try {
sApplyMethod = findApplyMethod();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
private static Method findApplyMethod() throws NoSuchMethodException {
Class clz = SharedPreferences.Editor.class;
return clz.getMethod("apply");
}
public static void apply(SharedPreferences.Editor editor) throws InvocationTargetException, IllegalAccessException {
if(sApplyMethod !=null){
sApplyMethod.invoke(editor);//invoke通常用來(lái)執(zhí)行一個(gè)方法
return;
}
editor.commit();
}
}
}