美好的一天,我有一個令人困惑的問題,它應該非常簡單。我有這個3類:當從另一個創建它們的活動調用時,SharedPreferences值不會從服務中檢索到
1.A config activity that creates and saves sharedpreferences.
2.A service.
3.Another Activity B
從配置活動,我保存以這種方式:
prefs = getSharedPreferences(MY_PREF_NAME, MODE_WORLD_READABLE);
boolean toggleCheck = toggleCelsius.isChecked();
boolean currentLocation = mylocation.isChecked();
boolean searchLocation = search.isChecked();
int updateSet = update.getSelectedItemPosition();
Editor editor = prefs.edit();
editor.putString(CITY + appWidgetId, city);
editor.putBoolean(GPS_LOCATION + appWidgetId, currentLocation);
editor.putBoolean(SEARCH_LOCATION + appWidgetId, searchLocation);
editor.putBoolean(CHECKED_CELSIUS + appWidgetId , toggleCheck);
editor.putInt(REFRESH_UPDATE + appWidgetId, updateSet);
editor.commit();
和i。從服務onCreate()方法檢索這種方式:
SharedPreferences prefs = this.getSharedPreferences(myCastConfigure.MY_PREF_NAME, MODE_WORLD_READABLE); //have tried using getApplicationContext()
city = prefs.getString(CITY + appWidgetId,"nothing");
chk_celsius = prefs.getBoolean(CHECKED_CELSIUS + appWidgetId, true);
updateRate = Configure.convertToMillis(prefs.getInt(REFRESH_UPDATE + appWidgetId, 1));
我保存首選項後,服務啓動,我可以從服務檢索sharedPreferences值。但是當我嘗試從活動B重新啓動服務時,我不檢索從第e服務了。相反,我得到的價值「沒有」。我曾嘗試從服務中使用getApplicationContext(),但這似乎也沒有幫助!任何人都可以請解釋發生了什麼,並提出建議。謝謝。
我有同樣的問題,聞起來像臭蟲......一切都在第一次嘗試確定,但第二...返回false,但價值是真實的... – keybee 2013-03-23 15:48:30