您好,登錄后才能下訂單哦!
怎么在Android中利用SharedPreferences實現存儲?很多新手對此不是很清楚,為了幫助大家解決這個難題,下面小編將為大家詳細講解,有這方面需求的人可以來學習下,希望你能有所收獲。
SharedPreferences 特點
即便是Android小白都知道的SharedPreferences的用法,這是保存數據最簡便的方法,但是不處理好的話后期維護將是一個巨大的坑。那么該如何處理好SharedPreferences才方便維護呢。先從它的特點開始入手吧。
難以維護的 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保存和讀取的方法非常簡便,正因此才不易維護,可能因為寫錯Key而找不出問題所在,當然一般我們都會把Key寫成靜態常量。但隨著項目增大,可能需要多處修改用戶信息,每次都要一個個put(), 除了寫著厭煩看著難受,當遇到需要增加或減少字段時也非常不便。
SharedPreferences 該這么寫
如果能保留原有寫法,又能規范方法那是最好不過了,看下SharedPreferences的接口方法并不多,很容易就寫個包裝類封裝下。
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(); } } }
接下來的使用就簡單了
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; } } }
調用即方便擴展,也保留了原有方法。
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);
看完上述內容是否對您有幫助呢?如果還想對相關知識有進一步的了解或閱讀更多相關文章,請關注億速云行業資訊頻道,感謝您對億速云的支持。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。