【发布时间】:2015-12-02 14:06:50
【问题描述】:
我收到了PreferenceActivity。当用户更改首选项时,我想保存一些额外的首选项,所以在 OnPreferencesChange 方法中,我得到了这样的内容:
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”值,但总是得到“otherKey”的“default2”。这是为什么?会不会是 Service 获取了错误的 SharedPreference?
【问题讨论】:
标签: android sharedpreferences preferenceactivity