views:

6828

answers:

5

How do I check if a background service (on Android) is running? I want an android activity that toggles the state of the service -- lets me turn it on if it is off and off if it is on.

A: 

You can query the intent, which service?

This is worth reading.. well basically all android dev docs.

Quintin Robinson
+5  A: 

I had the same problem not long ago. Since my service was local, I ended up simply using a static field in the service class to toggle state, as described by hackbod here:

http://groups.google.com/group/android-developers/browse_thread/thread/8c4bd731681b8331/bf3ae8ef79cad75d

miracle2k
This is what I ended up doing.
Bee
+7  A: 

Search for ActivityManager.RunningServiceInfo

A: 

Check out this german guide.

Markus Pielmeier
A: 

You can use this (i didnt try this yet, but hope this works)

if(startService(someIntent) != null) { 
    Toast.makeText(getBaseContext(), 'Service is already running', Toast.LENGTH_SHORT).show();
}else {
    Toast.makeText(getBaseContext(), 'There is no service running, starting service..', Toast.LENGTH_SHORT).show();
}

the startService method return a ComponentName object, if there is an already running service, if not null will returned.

http://developer.android.com/reference/android/content/Context.html#startService%28android.content.Intent%29

This is not like checking i think, because its starting the service, so you can add stopService(someIntent); under the code

Keenan Gebze