I am currently storing the last update time of my service in a Prefence:
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM HH:mm:ss");
String now = sdf.format(new Date());
Editor edit = PreferenceManager
.getDefaultSharedPreferences(this).edit();
edit.putString("lastUpdate", now);
edit.commit();
Log.i(this.getClass().toString(),"***** SERVICE: "+now+" *****");
// Done with our work... stop the service!
AlarmService_Service.this.stopSelf();
This is working pretty well, but the problem come when I want to display it in my PrefenceActivity.
In fact it seems that I have to force stop the app for the PreferenceActivity to display latest value.
Here is one exemple:
App started at 20:50
Service started at 20:50
=> PrefenceActivity displays 20h50...Sounds good
Service started at 21:00
=> PrefenceActivity still displays 20h50!!!
Service started at 21:10
=> PrefenceActivity still displays 20h50!!!
Then at 21:12,=> PrefenceActivity still displays 20h50!!!
21:12 I force close the app and open it again:
=> PrefenceActivity finally display latest value (21:10)
I really don't understand what happens, it seems that the Prefence is not updated during the Service!!!
public class PreferenceActivity extends SherlockPreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
setSummaryAll(this.getPreferenceScreen());
}
private void setSummaryAll(PreferenceScreen pScreen) {
for (int i = 0; i < pScreen.getPreferenceCount(); i++) {
Preference pref = pScreen.getPreference(i);
updatePref(pref);
}
}
public void updatePref(Preference pref) {
if (pref instanceof ListPreference) {
final ListPreference listPref = (ListPreference) pref;
listPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
listPref.setSummary(listPref.getEntries()[listPref
.findIndexOfValue(newValue.toString())]);
return true;
}
});
pref.setSummary(listPref.getEntry());
} else if (pref instanceof EditTextPreference) {
Log.i("***", "EditTextPreference " + pref.getKey());
final EditTextPreference etPref = (EditTextPreference) pref;
etPref.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference,
Object newValue) {
etPref.setSummary(newValue.toString());
return true;
}
});
pref.setSummary(etPref.getText());
}
}
}
I wrote: android:process=":remote"
That is the source of your difficulty. SharedPreferences
values are cached on a per-process basis, and one process will not know about another process' changes.
If removing it doesn't affect my app
It will mean that your app consumes less RAM, and possibly less CPU. Generally, you do not need more than one process in your app.