//SharedPreferences封裝工具類
public class SpUtils {
private static final String file_name = "sp_name";
private static int sp_mode = Context.MODE_PRIVATE;
private static Context context;
//存值的方法
public static boolean put(String key, Object value) {
SharedPreferences sp = context.getSharedPreferences(file_name, sp_mode);
SharedPreferences.Editor edit = sp.edit();
if (value instanceof String) {
if (!TextUtils.isEmpty((CharSequence) value)) {
edit.putString(key, (String) value);
}
} else if (value instanceof Boolean) {
edit.putBoolean(key, (Boolean) value);
} else if (value instanceof Float) {
edit.putFloat(key, (Float) value);
} else if (value instanceof Integer) {
edit.putInt(key, (Integer) value);
} else {
edit.putLong(key, (Long) value);
}
boolean commit = edit.commit();
return commit;
}
//取值的方法
public static String getString(String key, String defult) {
SharedPreferences sp = context.getSharedPreferences(file_name, sp_mode);
return sp.getString(key, defult);
}
public static int getInt(String key, Integer defult) {
SharedPreferences sp = context.getSharedPreferences(file_name, sp_mode);
return sp.getInt(key, defult);
}
public static boolean getBoolean(String key, Boolean defult) {
SharedPreferences sp = context.getSharedPreferences(file_name, sp_mode);
return sp.getBoolean(key, defult);
}
//銷毀
public static void remove(String key) {
SharedPreferences sp = context.getSharedPreferences(file_name, sp_mode);
SharedPreferences.Editor edit = sp.edit();
edit.remove(key);
edit.commit();
}
}
SharedPreferences封裝工具類
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 前段時間封裝了一個Preference的工具類,每次寫項(xiàng)目,都會或多或少的修改一點(diǎn),漸漸的改成了目前的樣子,覺得暫...
- 一、為什么封裝? 眾所周知,Android 從 6.0開始引入運(yùn)行時權(quán)限機(jī)制,將權(quán)限分為了普通權(quán)限和危險權(quán)限 ,對...
- ? 這是一個連recycler都不用實(shí)例化就可以用并添加各種特效的管理工具,目前包含了添加header,foo...
- K14 11/15 提問銷售法 不同的客戶可能因?yàn)椴煌脑驅(qū)νN產(chǎn)品產(chǎn)生興趣。提問銷售法認(rèn)為客戶有不同的購買動機(jī)...