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
2009-03-01 18:10:40
+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:
miracle2k
2009-03-03 22:58:35
This is what I ended up doing.
Bee
2009-03-04 18:41:43
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.
This is not like checking i think, because its starting the service, so you can add stopService(someIntent);
under the code
Keenan Gebze
2010-09-23 17:01:57