2014-11-25 65 views
0

我在首选项中保存一个值,但它总是给我默认值。当应用程序打开时,我可以得到实际的价值。但是当我从IntentService获得值时,它总是给我默认值。通过IntentService无法访问的共享首选项

代码保存价值:

prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE); 
Editor editor = prefs.edit(); 
editor.putString(key, value); 
editor.commit(); 

代码读取值:

prefs = context.getSharedPreferences(NAME, Context.MODE_PRIVATE); 
String value = prefs.getString(key, defaultValue); 

但是,如果我更改文件名,然后它的工作了一段时间,但后来它再次开始给予默认值

回答

0

从共享偏好设置中检索内容:

SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(Context); 
String s = sp.getString("key", null); // get "value" from the Shared Preferences 
+0

不幸运,相同的结果 – Hii 2014-11-25 11:59:14

0

尝试从Context.MODE_PRIVATE更改访问模式,因为您正在从外部读取它。 尝试 prefs = context.getSharedPreferences(NAME,Context.MODE_MULTI_PROCESS);