Synchronizing activity with service state

by bee » Thu, 12 Mar 2009 10:22:31 GMT


Sponsored Links
 I've got an activity that lets a user start and stop a service, which
then hangs around putting up notifications periodically. I have a
toggle button for turning the service on and off, and I need it to
have the correct state whenever the user runs the activity, depending
on whether the service is already running or not.

Since there doesn't appear to be a way to directly query whether or
not the service is running, my solution was to have the service class
set a boolean "preference" to true in its onCreate() method, and set
it to false in its onDestroy(). The launcher activity in turn checks
the preference set by the service in its onCreate() and sets the
toggle's state accordingly.

service class:
private void savePreference(boolean state) {
        SharedPreferences settings = getSharedPreferences(PING_PREFS,0);
        SharedPreferences.Editor editor = settings.edit();
        editor.putBoolean(KEY_RUNNING, state);
        editor.commit();
}
onCreate() {
    ...
    savePreference(true);
}
onDestroy() {
    ...
    savePreference(false);
}

and the toggle button does:
private OnClickListener mTogListener = new OnClickListener() {
         public void onClick(View v) {
            // Perform action on clicks
            if (tog.isChecked()) {
                Toast.makeText(TimepieController.this, "ON",
Toast.LENGTH_SHORT).show();
                startService(new Intent(TimepieController.this,
                                        PingService.class));
                mStarted = true;
            } else {
                Toast.makeText(TimepieController.this, "OFF",
Toast.LENGTH_SHORT).show();
                        stopService(new Intent(TimepieController.this,
                                        PingService.class));
                        mStarted = false;
            }
        }
};

So it seems to me like this ought to work, however, after I stop the
service if I leave the launcher activity with the "Back" button and
then return, the toggle button is set back to on again. If I leave the
launcher activity with the "Home" button when I return the toggle
button remains off.

Is there some better/more correct way to synchronize the state of the
toggle button with the service? If not, any ideas on what is going
wrong? Why would the way I leave the app effect the state of the
preference? Does stopping the service not necessarily call the
service's onDestroy()?

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. How to use phone internal memory to store user data......

absolutely..
if you store your data with in your application package then other
applications can't access that data.
you can go for a content provider if you want to share data between
applications.




-- 

2. W T A : aplikasi Islam

Teman, mau tanya aplikasi Muslim/Islam yang bagus dong, untuk aplikasi apa saja.

Thanks.


Sent from my Milestone by Axis Worry...

-- 
"Indonesian Android Community [id-android]" 

3. on configuration change landscape to portrait it always start activity from beginning.

4. Root Hero Sprint

5. How to detect when android kill my process?

6. W T A : Apk Bloo atau Babler untuk Facebook

7. RIL - Info