我不喜歡Android的SharedPreferences
類的工作方式。 我的主要原因是每次在應用程序的任何部分查找鍵值對時,都必須指定默認值。鑑於大多數應用程序可能會在多個地方調用這些值,這種做法真的有種促進預期應用/偏好的行爲錯誤,因爲沒有中心點來獲取你的默認設置。示例:Activity getBoolean(key133,false),Actvity B getBoolean(key133,true):問題。優先級覆蓋默認行爲
我通過指定/存儲在HashMap
我需要的應用程序的優先級值規避這個問題,我有一個自定義SharedPreferences
派生類,它懶洋洋地查找指定HashMap
S中的默認值。我只是打電話說MyClass.getBoolean(鍵)或MyClass.getString(鍵),它在HashMap
查找默認值,返回和存儲(.commit() - 荷蘭國際集團),如果不存在,就。默認行爲是撥打getBoolean(key, default_value)
。這對性能也是有益的,因爲在應用程序初始化時存儲所有首選項是非常糟糕的性能。
解決了一個問題,但現在我有一個新的問題:所有Preference
派生類都找不到默認值,因爲我無法使用我自己的SharedPreference
派生方式來查詢默認應用程序設置。這導致我的應用程序工作正常,但我的設置實際上不是顯示默認值,因爲它們不是存儲(總是),但在第一次打開設置頁...
長話短說:我怎麼重寫Preference
類阻止這個?
編輯:一些代碼來解釋這個問題,爲什麼我選擇了這條路
private final static Context context = ApplicationSubclass.getApplicationContext_();
private final static SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
private final static SharedPreferences.Editor editor = prefs.edit();
public static Map<String, Boolean> booleans = new HashMap<String, Boolean>();
public static Map<String, Integer> longs = new HashMap<String, Integer>();
public static Map<String, String> strings = new HashMap<String, String>();
public static Map<String, Integer> integers = new HashMap<String, Integer>();
public aPreferences(){
initialise();
}//end constructor
public void initialise(){
// setup default preferences
setBooleans();
//setLongs();
setStrings();
//setIntegers();
}//end method
private void setBooleans(){
booleans.put("does_this_work", true);
booleans.put("is_it_easy", true);
booleans.put("is_it_free_of_problems", false);
// and some hundreds more
}//end method
private void setStrings(){
strings.put("show_me", "Show me!");
// and some hundreds more
}//end method
// work the magic here
public boolean getBoolean(String key){
if (!prefs.contains(key)) putBoolean(key, booleans.get(key));
return prefs.getBoolean(key, booleans.get(key));
}//end method
public void putBoolean(String key, boolean value){
editor.putBoolean(key, value).commit();
}//end method
這工作:
private static aPreferences values = new aPreferences();
if (values.getBoolean("does_this_work")) // value initialised upon first access
Log.d(getClass().getSimpleName(), values.getString("show_me"));
這不:
CheckBoxPreference sound = new CheckBoxPreference(this);
sound.setKey("does_this_work"); // value not initialised, so checkbox not checked
sound.setTitle(getResources().getString(R.string.sound_title));
sound.setSummary(getResources().getString(R.string.sound_text));
root.addPreference(sound);
AFAIK,你沒有。首先,你必須重寫每個'Preference'子類。其次,我並沒有成功地讓首選項屏幕系統使用除Shared Shared的庫存實現以外的任何其他功能(例如,我自己的加密的SharedPreferences實現)。順便說一句,如果僅僅爲默認值使用靜態數據成員,或者調用'setDefaultValues()'來使用在首選項XML文件中定義的默認值,那麼是不是更簡單? – CommonsWare
這裏有一些好點。我可以使用靜態數據成員,但是我正在使用'HashMap's,因爲我確實使用了'setDefaultValues()'方法,它會迭代地圖並寫入值。但是我注意到,寫入首選項需要5秒的時間。所以現在我只是在第一次訪問時啓動了這些值,其中'Preference'類不理解...... – slinden77
加上,我有幾百個優先值...使用靜態數據成員只是有點麻煩 – slinden77