我有3个首选项文件,常规,设置和临时文件
我希望能够使用此类来编写给定类型的首选项,例如:
stg_full_screen: true // as boolean
这是我到目前为止所做的:
import androID.app.Activity;import androID.content.Context;import androID.content.SharedPreferences;public class SharedPrefManager extends Activity { // Globals private int GENERAL = 1000; private int SETTINGS = 2000; private int TEMP_STORE = 3000; private String PREF_GENERAL = "com.example.general"; private String PREF_SETTINGS = "com.example.settings"; private String PREF_TEMP_STORE = "com.example.temp_store"; private SharedPreferences general; private SharedPreferences settings; private SharedPreferences tempStore; private SharedPreferences.Editor general_editor; private SharedPreferences.Editor settings_editor; private SharedPreferences.Editor temp_store_editor; // Instantiate singleton object private static SharedPrefManager ourInstance = new SharedPrefManager(); public static SharedPrefManager getInstance() { return ourInstance; } private SharedPrefManager() { // Get handle on all preference files general = getSharedPreferences(PREF_GENERAL,Context.MODE_PRIVATE); settings = getSharedPreferences(PREF_SETTINGS,Context.MODE_PRIVATE); tempStore = getSharedPreferences(PREF_TEMP_STORE,Context.MODE_PRIVATE); // provision editors for all preference files general_editor = general.edit(); settings_editor = settings.edit(); temp_store_editor = tempStore.edit(); } private String read_prefs (String pref_name) { // this method reads a preference and returns it // IDeally,i would want to be able to return appropriate types by request // e.g boolean,string return null; } private voID write_prefs (String pref_name,String pref_val) { // this method would take a preference and write the appropriate type to prefs } // this method determines where to put a preference by checking the name of the key // this works because i use the following naming conventions // stg_name for settings,tmp_name for all that goes into tempStore private String resolve_pref_category (String path) { if (path.startsWith("stn")) return PREF_SETTINGS; else if (path.startsWith("tmp")) return PREF_TEMP_STORE; else return PREF_GENERAL; }}
我的问题是:
>这是明智的做法吗?
>我如何有效地确定偏好的类型?
谢谢
解决方法 通常,我使用这样的东西:没有静态上下文引用,每个属性的静态getter / setter,在需要时,您可以为某些属性添加内存缓存值,以便从内存中更快地获取它,而不是从SharedPreferences中读取.明确的API.
public class SharedPreferencesManager { private static final String APP_SETTINGS = "APP_SETTINGS"; // propertIEs private static final String SOME_STRING_VALUE = "SOME_STRING_VALUE"; // other propertIEs... private SharedPreferencesManager() {} private static SharedPreferences getSharedPreferences(Context context) { return context.getSharedPreferences(APP_SETTINGS,Context.MODE_PRIVATE); } public static String getSomeStringValue(Context context) { return getSharedPreferences(context).getString(SOME_STRING_VALUE,null); } public static voID setSomeStringValue(Context context,String newValue) { final SharedPreferences.Editor editor = getSharedPreferences(context).edit(); editor.putString(SOME_STRING_VALUE,newValue); editor.commit(); } // other getters/setters}总结
以上是内存溢出为你收集整理的编写Singleton类来管理Android SharedPreferences全部内容,希望文章能够帮你解决编写Singleton类来管理Android SharedPreferences所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)