I have a background service that reads cpu usage and frequency and displays it on notification bar
In application settings(Preferences) i have a option to chose to display only frequency only load or both
But method for getting shared preferences wont get most recent SharedPreference
it get SharedPreference only first time service starts and if i chose diferent option in Preference screen it wont update in service
Here is the code
@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;
}