我有一個後臺服務讀取CPU使用率和頻率,並顯示通知欄如何獲得最新sharedPreferences
在應用程序設置(首),我有一個選項可以選擇只顯示頻率只有負載或兩者上
但方法得到共享偏好不會得到最新SharedPreference
它得到SharedPreference只有第一次服務啓動,如果我選擇了diferent選項偏好屏幕它的服務不會更新
下面是代碼
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Runnable runnable = new Runnable() {@Override
public void run() {
while (thread) {
sharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
items = sharedPrefs.getString("notif", "freq");
System.out.println(items); //this keeps displaying the same value even if i go to Preference screen and change to something else
if (items.equals("freq") || items.equals("both")) {
}
if (items.equals("load") || items.equals("both")) {
} //reading frequency and load depending on what is selected
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
mHandler.post(new Runnable() {@Override
public void run() {
if (thread) {
createNotification(); //create notification
}
}
});
}
}
};
new Thread(runnable).start();
return START_STICKY;
}
你叫' commit()'在sharedPref的編輯器上? –
不要給標的添加標籤 –
我不需要。我使用PreferenceActivity。如果我看起來像/data/data/myapp/shared_prefs/sharedPrefs_file.xml中的manualy值是新鮮的,但在服務中,它始終顯示舊值 – pedja