2021-07-11

SharedPreferences是 Android 平臺為應用開發(fā)者提供的一個輕量級的存儲輔助類,用來保存應用的一些常用配置,它提供了 putString()、putString(Set<String>)、putInt()、putLong()、putFloat()、putBoolean() 六種數(shù)據(jù)類型。在應用中通常做一些簡單數(shù)據(jù)的持久化存儲,通常用來保存各種配置信息,其本質(zhì)是一個以“鍵-值”對的方式保存數(shù)據(jù)的xml文件,其文件保存在/data/data//shared_prefs目錄下

SharedPreferences的四種操作模式

Context.MODE_PRIVATE:為默認操作模式,代表該文件是私有數(shù)據(jù),只能被應用本身訪問,在該模式下,寫入的內(nèi)容會覆蓋原文件的內(nèi)容

Context.MODE_APPEND:模式會檢查文件是否存在,存在就往文件追加內(nèi)容,否則就創(chuàng)建新文件。

MODE_WORLD_READABLE:表示當前文件可以被其他應用讀取。

MODE_WORLD_WRITEABLE:表示當前文件可以被其他應用寫入。

/**

* 保存到本地的配置文件

*

* @author VinPin

*/

public class SharedPreference {

? ? private static String FILLNAME = "config";// 文件名稱

? ? private static SharedPreferences mSharedPreferences = null;

? ? public final static String PRE_ACCOUNT = "account";

? ? public final static String PRE_USERID = "userid";

? ? public final static String PRE_TOKENID = "tokenid";

? ? public final static String PRE_USERNAME = "username";

? ? public final static String PRE_USEREMAIL = "useremail";

? ? public final static String PRE_PHONE = "phone";

? ? /**

? ? * 單例模式

? ? */

? ? public static synchronized SharedPreferences getInstance(Context context) {

? ? ? ? if (mSharedPreferences == null) {

? ? ? ? ? ? mSharedPreferences = context.getApplicationContext().getSharedPreferences(FILLNAME, Context.MODE_PRIVATE);

? ? ? ? }

? ? ? ? return mSharedPreferences;

? ? }

? ? /**

? ? * SharedPreferences常用的10個操作方法

? ? */

? ? public static void putBoolean(String key, boolean value, Context context) {

? ? ? ? SharedPreference.getInstance(context).edit().putBoolean(key, value).apply();

? ? }

? ? public static boolean getBoolean(String key, boolean defValue, Context context) {

? ? ? ? return SharedPreference.getInstance(context).getBoolean(key, defValue);

? ? }

? ? public static void putString(String key, String value, Context context) {

? ? ? ? SharedPreference.getInstance(context).edit().putString(key, value).apply();

? ? }

? ? public static String getString(String key, String defValue, Context context) {

? ? ? ? return SharedPreference.getInstance(context).getString(key, defValue);

? ? }

? ? public static void putInt(String key, int value, Context context) {

? ? ? ? SharedPreference.getInstance(context).edit().putInt(key, value).apply();

? ? }

? ? public static int getInt(String key, int defValue, Context context) {

? ? ? ? return SharedPreference.getInstance(context).getInt(key, defValue);

? ? }

? ? /**

? ? * 移除某個key值已經(jīng)對應的值

? ? */

? ? public static void remove(String key, Context context) {

? ? ? ? SharedPreference.getInstance(context).edit().remove(key).apply();

? ? }

? ? /**

? ? * 清除所有內(nèi)容

? ? */

? ? public static void clear(Context context) {

? ? ? ? SharedPreference.getInstance(context).edit().clear().apply();

? ? }

}

?著作權歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容