How do I check if a background service (on Android) is running?
I want an Android activity that toggles the state of the service -- it lets me turn it on if it is off and off if it is on.
How do I check if a background service (on Android) is running?
I want an Android activity that toggles the state of the service -- it lets me turn it on if it is off and off if it is on.
There can be several services with the same class name.
I've just created two apps. The package name of the first app is
com.example.mock
. I created a subpackage calledlorem
in the app and a service calledMock2Service
. So its fully qualified name iscom.example.mock.lorem.Mock2Service
.Then I created the second app and a service called
Mock2Service
. The package name of the second app iscom.example.mock.lorem
. The fully qualified name of the service iscom.example.mock.lorem.Mock2Service
, too.Here is my logcat output.
A better idea is to compare
ComponentName
instances becauseequals()
ofComponentName
compares both package names and class names. And there can't be two apps with the same package name installed on a device.The equals() method of
ComponentName
.ComponentName
Take it easy guys... :)
I think the most suitable solution is holding a key-value pair in
SharedPreferences
about if the service is running or not.Logic is very straight; at any desired position in your service class; put a boolean value which will act as a flag for you about whether the service is running or not. Then read this value whereever you want in your application.
A sample code which I am using in my app is below:
In my Service class (A service for Audio Stream), I execute the following code when the service is up;
Then in any activity of my application, I am checking the status of the service with the help of following code;
No special permissions, no loops... Easy way, clean solution :)
If you need extra information, please refer the link
Hope this helps.
Inside TheServiceClass define:
Then In onStartCommand(...)
Then, call
if(TheServiceClass.serviceRunning == true)
from any class.This applies more towards Intent Service debugging since they spawn a thread, but may work for regular services as well. I found this thread thanks to Binging
In my case, I played around with the debugger and found the thread view. It kind of looks like the bullet point icon in MS Word. Anyways, you don't have to be in debugger mode to use it. Click on the process and click on that button. Any Intent Services will show up while they are running, at least on the emulator.
The proper way to check if a service is running is to simply ask it. Implement a BroadcastReceiver in your service that responds to pings from your activities. Register the BroadcastReceiver when the service starts, and unregister it when the service is destroyed. From your activity (or any component), send a local broadcast intent to the service and if it responds, you know it's running. Note the subtle difference between ACTION_PING and ACTION_PONG in the code below.
simple use bind with don't create auto- see ps. and update...example :
why not using? getRunningServices()
Note: this method is only intended for debugging or implementing service management type user interfaces.
ps. android documentation is misleading i have opened an issue on google tracker to eliminate any doubts:
https://issuetracker.google.com/issues/68908332
as we can see bind service actually invokes a transaction via ActivityManager binder through Service cache binders - i dint track which service is responsible for binding but as we can see the result for bind is:
transaction is made through binder:
next:
this is set in ActivityThread via:
this is called in ActivityManagerService in method:
then:
but there is no "activity" only window package and alarm..
so we need get back to call:
this makes call through:
which leads to :
and this is native method....
i don't have time now to dug in c so until i dissect rest call i suspend my answer.
but best way for check if service is running is to create bind (if bind is not created service not exist) - and query the service about its state through the bind (using stored internal flag on it state).
update 23.06.2018
i found those interesting:
in short :)
"Provide a binder to an already-bound service. This method is synchronous and will not start the target service if it is not present."
public IBinder peekService(Intent service, String resolvedType, String callingPackage) throws RemoteException;