Android SharedPreferences存储的正确写法
SharedPreferences特点
即便是Android小白都知道的SharedPreferences的用法,这是保存数据最简便的方法,但是不处理好的话后期维护将是一个巨大的坑。那么该如何处理好SharedPreferences才方便维护呢。先从它的特点开始入手吧。
- 通过Context.getSharedPreferences()获取的SharedPreferences是一个单例
- SharedPreferences.edit()每次都会创建一个新的编辑对象,commit()之前一切改动都无效
SharedPreferences的具体实现并不在公开API内,创建方法只有通过Context获取。具体实现我们不去研究,通过Log打印每次获取的对象都是同一个,同样的方法可知道每次edit()都会得到不同的对象。
难以维护的SharedPreferences
SharedPreferenceuserPreferences=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的接口方法并不多,很容易就写个包装类封装下。
importandroid.content.SharedPreferences; importandroid.support.annotation.Nullable; importjava.util.Map; importjava.util.Set; /** *SharedPreferences包装类 *Createdbywittytutuon17-3-29. */ publicclassPreferencesWrapperimplementsSharedPreferences{ protectedSharedPreferencesmPreferences; publicPreferencesWrapper(SharedPreferencespreferences){ mPreferences=preferences; } @Override publicMapgetAll(){ returnmPreferences.getAll(); } @Nullable @Override publicStringgetString(Stringkey,@NullableStringdefValue){ returnmPreferences.getString(key,defValue); } @Nullable @Override publicSetgetStringSet(Stringkey,@NullableSetdefValues){ returnmPreferences.getStringSet(key,defValues); } @Override publicintgetInt(Stringkey,intdefValue){ returnmPreferences.getInt(key,defValue); } @Override publiclonggetLong(Stringkey,longdefValue){ returnmPreferences.getLong(key,defValue); } @Override publicfloatgetFloat(Stringkey,floatdefValue){ returnmPreferences.getFloat(key,defValue); } @Override publicbooleangetBoolean(Stringkey,booleandefValue){ returnmPreferences.getBoolean(key,defValue); } @Override publicbooleancontains(Stringkey){ returnmPreferences.contains(key); } @Override publicEditorWrapperedit(){ returnnewEditorWrapper(mPreferences.edit()); } @Override publicvoidregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListenerlistener){ mPreferences.registerOnSharedPreferenceChangeListener(listener); } @Override publicvoidunregisterOnSharedPreferenceChangeListener(OnSharedPreferenceChangeListenerlistener){ mPreferences.unregisterOnSharedPreferenceChangeListener(listener); } publicstaticclassEditorWrapperimplementsEditor{ protectedEditormEditor; publicEditorWrapper(Editoreditor){ mEditor=editor; } @Override publicEditorWrapperputString(Stringkey,@NullableStringvalue){ mEditor.putString(key,value); returnthis; } @Override publicEditorWrapperputStringSet(Stringkey,@NullableSetvalues){ mEditor.putStringSet(key,values); returnthis; } @Override publicEditorWrapperputInt(Stringkey,intvalue){ mEditor.putInt(key,value); returnthis; } @Override publicEditorWrapperputLong(Stringkey,longvalue){ mEditor.putLong(key,value); returnthis; } @Override publicEditorWrapperputFloat(Stringkey,floatvalue){ mEditor.putFloat(key,value); returnthis; } @Override publicEditorWrapperputBoolean(Stringkey,booleanvalue){ mEditor.putBoolean(key,value); returnthis; } @Override publicEditorWrapperremove(Stringkey){ mEditor.remove(key); returnthis; } @Override publicEditorWrapperclear(){ mEditor.clear(); returnthis; } @Override publicbooleancommit(){ returnmEditor.commit(); } @Override publicvoidapply(){ mEditor.apply(); } } }
接下来的使用就简单了
publicclassUserInfoPreferencesextendsPreferencesWrapper{ privatestaticfinalStringNAME="UserInfo"; publicstaticfinalKEY_ID="id" publicstaticfinalKEY_NAME="name"; publicstaticfinalKEY_HEAD="head"; publicstaticfinalKEY_SEX="sex"; privatestaticvolatileUserInfoPreferencesmPreferences; publicstaticUserInfoPreferencesgetInstance(Contextcontext){ if(mPreferences==null){ synchronized(UserInfoPreferences.class){ if(mPreferences==null){ SharedPreferencespreferences=context.getApplicationCtontext().getShaedPreferences(NAME,Context.MODE_PRIVATE); mPreferences=newUserInfoPreferences(preferences); } } } returnmPreferences; } publicUserInfoPreferences(SharedPreferencespreferences){ super(preferences); } //获取用户信息 publicUserInfogetUserInfo(){ Stringid=mPreferences.getString(KEY_ID,""); Stringname=mPreferences.getString(KEY_NAME,""); Stringhead=mPreferences.getString(KEY_HEAD,""); returnnewUserInfo(id,name,head); } @Override publicEditoredit(){ returnnewEditor(mPreferences.edit()); } publicstaticclassEditorextendsEditorWrapper{ publicEditor(SharedPreferences.Editoreditor){ super(editor); } //保存用户信息 publicEditorputUserInfo(Stringid,Stringname,Stringhead){ mEditor.putString(KEY_ID,id) .putString(KEY_AME,name) .putString(KEY_HEAD,head); returnthis; } //清空用户信息 publicEditorclearUserInfo(){ mEditor.remove(KEY_ID) .remove(KEY_NAME) .remove(KEY_HEAD); returnthis; } } }
调用即方便扩展,也保留了原有方法。
UserInfoPreferencespreferences=UserInfoPreferences.getInstance(context); preferences.edit() .clearUserInfo() .putUserInfo("myId","myName","myHead") .putInt(KEY_SEX,0) .commit(); UserInfouserInfo=preferences.getUserInfo(); intsex=preferences.getInt(KEY_SEX,0);
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!