I want to create a single button which serves both functions of Starting and Stopping a service.
Also I want to make sure that even if user quits the application and again comes back then according to whether service is running or not, I want to show appropriate Text on Button.
So in short, With what thing I can get to know if service is running or not ?
You can check if service is running or not with below code. Rest of the question is logic based once you find the service is running or not.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.MyService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I refer this answer just check it
Related
My question is very simple. I have a foreground service that, when activated, runs indefinitely until the user stops it. In the I MainActivity, I need to know if the service is running or not (image if the user closes the app but the foreground service is still running, when he reenters the app, I need to know if the service is already running). Is it viable to have a companion object on the Service with a status variable so that I can access its status?
Something like this:
#AndroidEntryPoint
class SomeForegroundService: Service() {
companion object {
var status = 0
}
....
And then somewhere in my MainActivity...
SomeForegroundService.status == 1
Is this prone to memory leaks? Whats a better solution (not counting with checking every running system service)
What you need is to bind the service
https://developer.android.com/guide/components/bound-services
If you use bindService(intent, connection, 0) without the Context.BIND_AUTO_CREATE flag, it will only bind to existing service, and return false if the service does not exist.
You can use this method to check is service is running or not instead of static variable
// check and return true if service is running
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (TrackingService.class.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Edit :
If you don't want to check every service, you can register broadcast receiver in the service and it pings the activity.
Original Answer by : How to check if a service is running on Android? (answer by Ben H)
I have a android service extends from Service and onStartCommand is return START_STICKY. The service is self running service and no one bind on it.
In my MainActivity, I am using
Intent startService = new Intent(getApplicationContext(), MyService.class);
startService(startService);
Everything is working fine except in Setting -> Running Service 'show cached process' tab not shown anything for my application but in 'cached background process' tab show "Cached services in use". I doubt that why no shown in 'show cached process' tab but I see other app has shown?
The reason is that your service is running on the same process as the application. If you set android:process=":yourServiceName" in the manifest file, your service will start a new process and you can see your service.
Also, do check if your service is actually running using this method
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I have an app which has a single activity which has a checkbox the starts and stops a foreground service. The activity also has some other settings which control that service.
The problem is that after some time navigating other apps the service continues to run fine but the activity gets killed. If I restart the activity the checkbox will be set to default, off. If I switch it to on, a second service starts, while the first one is still running and can't be controlled anymore.
I can't figure out how to handle this situation. It's necessary for the service to run indefinitely and there doesn't seem to be anyway to keep the activity from dying. Maybe there's a way for the new activity to find the old service?
Try to kill the service in OnPause() method of your activity .that solve my problem few times ago.
To check if particular service is running, write the below method your activity, and call it when you want to check the state of the service
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
"service continues to run fine but the activity gets killed"
services are given more importance than activities, if android where given a choice to kill an activity or a service it always kill an activity first.
"a second service starts"
services can only be started once no matter how many times you start a service.
"checkbox will be set to default"
use sharedpreferences to persist values.
I have a Foreground Service for my app, which must run the whole time and never stops. While I was testing it few times, it seems after 3-4 days Android decides it just to stop it, and never restart it again, and in my Activity I have a check for the service if its running, and if it isnt to start it.
Even though, the service is not starting, and I can't debug it, because this is not happening immidiatelly.
So this is my check, is there something wrong in it?
if (!isMyServiceRunning(MyService.class)) {
startService(new Intent(this, MyService.class));
}
private boolean isMyServiceRunning(Class<?> serviceClass) {
ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if (serviceClass.getName().equals(service.service.getClassName())) {
return true;
}
}
return false;
}
I call this check in onResume() of the Activity, and I can't just run it everytime when the activity starts, because I have a constantly running Thread in the service, which will get more than one instance if started several times...
So here is what I want:
Either have more reliable way to check for the service if its running
or
Just start the service every time the activity starts, to prevent problems, and the thread inside of it must always have just 1 instance (I have already declared is as static)
You can make your service Bindable and bind your Activity to the service when the Activity starts/resumes. This way you will be sure the service is running. But I think you have a problem with your service. My I ask you to post more information about the service? It is not normal the OS to kill a foreground service with no traces.
what is the best way to implements a service from starting with alarm manager when android applications first starts?
Right now i have a Home activity which is the first page of the activity which runs the service when it is being created. However when the users were to start the application again on the app menu (when the application is running.), the home activity will be run again and the service will start again.
Is there any better alternative for this way of application?
Follow this link... have a look at the implemenation below.
http://blog.gregfiumara.com/archives/82 ...
and for the checking whether a service is active or not... you can simply use this.
private boolean isMyServiceRunning() {
ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
if ("com.example.background.BootableService".equals(service.service.getClassName())) {
return true;
}
}
return false;
}
Use this in the any class you want to check for a existing service... and if no service is present then just create one or else you are good.
Let me know how it works out.