0

I made an application that fires a long-lived background service. Sometimes the application itself is closed, and the service keeps running, which is the way I want it.

The problem is that I don't know how to control that service after the caller application is closed. For example if the users re-opened the app, I'd like to give them the option whether to kill that process or keep it running.

I am able to detect if the service is running by using the following function (taken from this solution):

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 tried then calling this line from the activity, but that didn't do anything:

if(isMyServiceRunning(MyService.class)) {
    stopService(new Intent(this, MyService.class));
}
Lamar
  • 1,761
  • 4
  • 24
  • 50
  • Either call `startService()` to send a command to the service (and also starting it if the service is not already running), or call `bindService()` to bind to the service and get an API that you can use to control the service. – CommonsWare Dec 02 '18 at 14:28
  • I would like the srevice not to bound to the activity, since the activity might be killed. Also I added some more details to the question showing stopService did not work. startService on the other hand would start a new instance of the service. – Lamar Dec 02 '18 at 14:31
  • 1
    "I would like the srevice not to bound to the activity, since the activity might be killed" -- the service is not bound to the activity. The activity is bound to the service. "stopService did not work" -- if your service is named `MyService` and is in this app, then the service was stopped. I would delete `isMyServiceRunning()` (as it is pointless). If you still see work being done by your service after stopping it, perhaps you did not clean up the service in `onDestroy()`, such as by terminating any background threads. – CommonsWare Dec 02 '18 at 14:33
  • Can I still access the service without the "isMyServiceRunning" function, even if the application that started the service was killed, and the app was re-launched? – Lamar Dec 02 '18 at 14:41
  • 1
    "Can I still access the service without the "isMyServiceRunning"" -- `isMyServiceRunning()` has nothing to do with your ability to access the service. "even if the application that started the service was killed" -- I assume that by "the application... was killed" you mean "the process was terminated". If so, and if the service is running in another process, then some future instance of your UI process can work with that service in the same way that your original UI process did (`startService()` or `bindService()`). – CommonsWare Dec 02 '18 at 14:44
  • You assumed right, I will try using onDestroy to clean up the service pending work and see if that fixes it. Thanks a lot! – Lamar Dec 02 '18 at 14:47
  • It worked, thanks a lot! Now, I really want to check the background service on every launch of the app. If "isMyServiceRunning" is not advised, what alternative can I use to check the state of the service? – Lamar Dec 02 '18 at 14:59
  • "If "isMyServiceRunning" is not advised" -- it's more that it is not necessary for being able to stop the service. It also is not atomic, as the service might stop milliseconds after you check its status. I usually view that sort of method as being a code smell, as there is probably a better solution for solving the actual business requirement. But, if you are willing to live with its limitations, and you really want to use it, go right ahead. – CommonsWare Dec 02 '18 at 15:02
  • I just need to give the user the status of the service, as well as control on whether to keep it running or stopping it. The service is intended to be kept running, till the user stops it, or it finishes it's job (could take hours to finish). – Lamar Dec 02 '18 at 15:08

0 Answers0