0

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());
        }
    }
}
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • 2
    Did you happen to set these up as separate processes, by any chance? – CommonsWare Nov 21 '12 at 19:48
  • how do I know if these set up are made in different process? I guess a Service is always a different process than the app itself? – Waza_Be Nov 21 '12 at 19:49
  • 3
    "how do I know if these set up are made in different process?" -- because when you wrote the app, you set it up that way, via `android:process` attributes in the manifest. "I guess a Service is always a different process than the app itself?" -- no. – CommonsWare Nov 21 '12 at 19:52
  • Indeed, I wrote: android:process=":remote" If removing it doesn't affect my app, please post the answer, so I can accept it ;-) – Waza_Be Nov 21 '12 at 19:53

1 Answers1

3

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Hi, just for the sake of knowledge, when do you believe using more than one process might be good for your app? – alfongj Nov 21 '12 at 20:01
  • He meant that removing will be good, not using both processes ;-) – Waza_Be Nov 21 '12 at 20:03
  • @pepillo: Use two processes if there is some platform/device bug that causes occasional crashes, that you feel you need to isolate into a separate process. 99.9% of apps should not need multiple processes, IMHO. – CommonsWare Nov 21 '12 at 20:03
  • @CommonsWare this will reflect vice verse means if i change preferenace value from activity it will not reflect in service if i given process attribute to service? – Hardik Feb 18 '15 at 12:41
  • @Hardik: Yes, preferences are not automatically propagated between processes. – CommonsWare Feb 20 '15 at 23:39