我收到了PreferenceActivity
。當用戶正在改變的偏愛,我想節省一些額外優惠,因此OnPreferencesChange
方法中,我得到了這樣的事情:在PreferenceActivity之外獲取偏好
if(p.getKey().equals("mykey")) //there is no issue with this if. it enters and get inside to the commit command
{
getPreferences(MODE_PRIVATE).edit().putString("otherKey","value").commit();
return true;
}
我也得到一個服務(這當然是不同的類比的PreferenceActivity),我想在其中閱讀首選項。所以我在做這樣的事情:
sp = PreferenceManager.getDefaultSharedPreferences();
String val1 = dsp.getString("myKey","default1");
String val2 = dsp.getString("otherKey","default2");
我得到「的myKey」的正確價值,但總是得到「默認設置2」爲「otherKey」。這是爲什麼?難道服務會得到錯誤的SharedPreference?