怎么在Android中利用SharedPreferences實(shí)現(xiàn)存儲(chǔ)?很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來(lái)學(xué)習(xí)下,希望你能有所收獲。
創(chuàng)新互聯(lián)服務(wù)項(xiàng)目包括達(dá)坂城網(wǎng)站建設(shè)、達(dá)坂城網(wǎng)站制作、達(dá)坂城網(wǎng)頁(yè)制作以及達(dá)坂城網(wǎng)絡(luò)營(yíng)銷策劃等。多年來(lái),我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢(shì)、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,達(dá)坂城網(wǎng)站推廣取得了明顯的社會(huì)效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到達(dá)坂城省份的部分城市,未來(lái)相信會(huì)繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
SharedPreferences 特點(diǎn)
即便是Android小白都知道的SharedPreferences的用法,這是保存數(shù)據(jù)最簡(jiǎn)便的方法,但是不處理好的話后期維護(hù)將是一個(gè)巨大的坑。那么該如何處理好SharedPreferences才方便維護(hù)呢。先從它的特點(diǎn)開(kāi)始入手吧。
難以維護(hù)的 SharedPreferences
SharedPreference userPreferences = context.getSharedPreferences("UserInfo", Context.MODE_PRIVATE); userPreferences.edit() .putString("id", "myId") .putString("name", "myName") .putString("head", "myHead") .commit(); Log.e("id", userPreferences.getString("id","")); Log.e("name", userPreferences.getString("name","")); Log.e("head", userPreferences.getString("head",""));
如上一段SharedPreferences保存和讀取的方法非常簡(jiǎn)便,正因此才不易維護(hù),可能因?yàn)閷戝e(cuò)Key而找不出問(wèn)題所在,當(dāng)然一般我們都會(huì)把Key寫成靜態(tài)常量。但隨著項(xiàng)目增大,可能需要多處修改用戶信息,每次都要一個(gè)個(gè)put(), 除了寫著厭煩看著難受,當(dāng)遇到需要增加或減少字段時(shí)也非常不便。
SharedPreferences 該這么寫
如果能保留原有寫法,又能規(guī)范方法那是最好不過(guò)了,看下SharedPreferences的接口方法并不多,很容易就寫個(gè)包裝類封裝下。
import android.content.SharedPreferences; import android.support.annotation.Nullable; import java.util.Map; import java.util.Set; /** * SharedPreferences 包裝類 * Created by wittytutu on 17-3-29. */ public class PreferencesWrapper implements SharedPreferences { protected SharedPreferences mPreferences; public PreferencesWrapper(SharedPreferences preferences) { mPreferences = preferences; } @Override public Map getAll() { return mPreferences.getAll(); } @Nullable @Override public String getString(String key, @Nullable String defValue) { return mPreferences.getString(key, defValue); } @Nullable @Override public Set getStringSet(String key, @Nullable Set defValues) { return mPreferences.getStringSet(key, defValues); } @Override public int getInt(String key, int defValue) { return mPreferences.getInt(key, defValue); } @Override public long getLong(String key, long defValue) { return mPreferences.getLong(key, defValue); } @Override public float getFloat(String key, float defValue) { return mPreferences.getFloat(key, defValue); } @Override public boolean getBoolean(String key, boolean defValue) { return mPreferences.getBoolean(key, defValue); } @Override public boolean contains(String key) { return mPreferences.contains(key); } @Override public EditorWrapper edit() { return new EditorWrapper(mPreferences.edit()); } @Override public void registerOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { mPreferences.registerOnSharedPreferenceChangeListener(listener); } @Override public void unregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListener listener) { mPreferences.unregisterOnSharedPreferenceChangeListener(listener); } public static class EditorWrapper implements Editor { protected Editor mEditor; public EditorWrapper(Editor editor) { mEditor = editor; } @Override public EditorWrapper putString(String key, @Nullable String value) { mEditor.putString(key, value); return this; } @Override public EditorWrapper putStringSet(String key, @Nullable Set values) { mEditor.putStringSet(key, values); return this; } @Override public EditorWrapper putInt(String key, int value) { mEditor.putInt(key, value); return this; } @Override public EditorWrapper putLong(String key, long value) { mEditor.putLong(key, value); return this; } @Override public EditorWrapper putFloat(String key, float value) { mEditor.putFloat(key, value); return this; } @Override public EditorWrapper putBoolean(String key, boolean value) { mEditor.putBoolean(key, value); return this; } @Override public EditorWrapper remove(String key) { mEditor.remove(key); return this; } @Override public EditorWrapper clear() { mEditor.clear(); return this; } @Override public boolean commit() { return mEditor.commit(); } @Override public void apply() { mEditor.apply(); } } }
接下來(lái)的使用就簡(jiǎn)單了
public class UserInfoPreferences extends PreferencesWrapper { private static final String NAME = "UserInfo"; public static final KEY_ID = "id" public static final KEY_NAME = "name"; public static final KEY_HEAD = "head"; public static final KEY_SEX = "sex"; private static volatile UserInfoPreferences mPreferences; public static UserInfoPreferences getInstance(Context context){ if(mPreferences == null) { synchronized (UserInfoPreferences.class){ if(mPreferences == null) { SharedPreferences preferences = context.getApplicationCtontext().getShaedPreferences(NAME, Context.MODE_PRIVATE); mPreferences = new UserInfoPreferences(preferences); } } } return mPreferences; } public UserInfoPreferences(SharedPreferences preferences) { super(preferences); } // 獲取用戶信息 public UserInfo getUserInfo(){ String id = mPreferences.getString(KEY_ID, ""); String name = mPreferences.getString(KEY_NAME, ""); String head = mPreferences.getString(KEY_HEAD, ""); return new UserInfo(id, name, head); } @Override public Editor edit(){ return new Editor(mPreferences.edit()); } public static class Editor extends EditorWrapper { public Editor(SharedPreferences.Editor editor) { super(editor); } // 保存用戶信息 public Editor putUserInfo(String id, String name, String head) { mEditor.putString(KEY_ID, id) .putString(KEY_AME, name) .putString(KEY_HEAD, head); return this; } // 清空用戶信息 public Editor clearUserInfo() { mEditor.remove(KEY_ID) .remove(KEY_NAME) .remove(KEY_HEAD); return this; } } }
調(diào)用即方便擴(kuò)展,也保留了原有方法。
UserInfoPreferences preferences = UserInfoPreferences.getInstance(context); preferences.edit() .clearUserInfo() .putUserInfo("myId", "myName", "myHead") .putInt(KEY_SEX, 0) .commit(); UserInfo userInfo = preferences.getUserInfo(); int sex = preferences.getInt(KEY_SEX, 0);
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,感謝您對(duì)創(chuàng)新互聯(lián)的支持。
網(wǎng)頁(yè)標(biāo)題:怎么在Android中利用SharedPreferences實(shí)現(xiàn)存儲(chǔ)
URL鏈接:http://www.rwnh.cn/article8/igisop.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動(dòng)態(tài)網(wǎng)站、手機(jī)網(wǎng)站建設(shè)、虛擬主機(jī)、關(guān)鍵詞優(yōu)化、品牌網(wǎng)站設(shè)計(jì)、微信小程序
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)