Android中SharedPreferences共享參數的封裝

Android中存在五種數據存儲方式:

一:SharedPreferences共享參數存儲

二:SQLite數據存儲

三:File文件存儲

四:ContentProvider內容提供者

五:網絡存儲。

在項目中也是經常用到這幾大存儲方式來進行數據存儲,項目中會不止一次的用到這些存儲方式中的某一個或者某幾個,要是每次用到的時候都去寫一次,這不免很麻煩,代碼也很不清晰,這個時候就需要對相應的方式進行自己的封裝,讓它能在項目中的任何時候都可以使用。接下來我就分享我在使用SharedPreferences的一些封裝和對SharedPreferences的認識:

SharedPreferences的四種操作模式:

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

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

Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有權限讀寫該文件

Context.MODE_WORLD_READABLE:當前文件可以被讀取

Context.MODE_WORLD_WRITEABLE:當前文件可以被寫入

public class SharedPreferencesUtils {private SharedPreferencesmPreferences;? ? private static SharedPreferencesUtilssharedPreferencesUtils;? ? private SharedPreferencesUtils(Context context) {if (mPreferences ==null) {mPreferences = context.getSharedPreferences("dong", 0);? ? ? ? }? ? }/**? ? * 獲取單實例對象? ? */? ? public static SharedPreferencesUtilsgetInstance(Context context) {if (sharedPreferencesUtils ==null) {sharedPreferencesUtils =new SharedPreferencesUtils(context);? ? ? ? }return sharedPreferencesUtils;? ? }/**? ? * 存儲String? ? */? ? public static void putString(SharedPreferencesUtils mPreferences, String key,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? String v) {? ? ? ? mPreferences.mPreferences.edit().putString(key, v).commit();? ? }/**? ? * 取String? ? */? ? public static StringgetString(Context context, String key) {return getInstance(context).mPreferences.getString(key, "");? ? }/**? ? * 存儲String? ? */? ? public static void putString(Context context, String key, String v) {getInstance(context).mPreferences.edit().putString(key, v).commit();? ? }/**? ? * 取String? ? */? ? public static StringgetString(SharedPreferencesUtils mPreferences, String key) {return mPreferences.mPreferences.getString(key, "");? ? }/**? ? * 取int? ? */? ? public static int getInt(SharedPreferencesUtils mPreferences, String key) {return mPreferences.mPreferences.getInt(key, -1);? ? }/**? ? * 取int? ? */? ? public static int getInt(Context context, String key) {return getInstance(context).mPreferences.getInt(key, -1);? ? }/**? ? * 存儲String? ? */? ? public static void putInt(SharedPreferencesUtils mPreferences, String key,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? int v) {? ? ? ? mPreferences.mPreferences.edit().putInt(key, v).commit();? ? }/**? ? * 存儲String? ? */? ? public static void putInt(Context context, String key, int v) {? ? ? ? SharedPreferencesUtils.getInstance(context).mPreferences.edit().putInt(key, v).commit();? ? }/**? ? * 取Long? ? */? ? public static long getLong(Context context, String key) {return getInstance(context).mPreferences.getLong(key, -1);? ? }/**? ? * 存儲String? ? */? ? public static void putLong(Context context, String key,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? long v) {getInstance(context).mPreferences.edit().putLong(key, v).commit();? ? }/**? ? * 存儲String? ? */? ? public static void putLong(Context context, String key, String v) {long l =0;? ? ? ? try {? ? ? ? ? ? l = Long.parseLong(v);? ? ? ? }catch (NumberFormatException e) {? ? ? ? ? ? e.printStackTrace();? ? ? ? }getInstance(context).mPreferences.edit().putLong(key, l).commit();? ? }/**? ? * 取Long? ? */? ? public static long getLong(SharedPreferencesUtils mPreferences, String key) {return mPreferences.mPreferences.getLong(key, -1);? ? }/**? ? * 存儲String? ? */? ? public static void putLong(SharedPreferencesUtils mPreferences, String key,? ? ? ? ? ? ? ? ? ? ? ? ? ? ? long v) {? ? ? ? mPreferences.mPreferences.edit().putLong(key, v).commit();? ? }}




https://blog.csdn.net/qq_34942689/article/details/65633451

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

相關閱讀更多精彩內容

友情鏈接更多精彩內容