2

I want to check whether my service is running in or not, I was using below code which was working fine till nougat but not working in OREO

 private boolean isMyServiceRunning(Class<?> serviceClass, Context context) {
        ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            Log.d(TAG, "isMyServiceRunning: " + service.service.getClassName());
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }

But above code is not working for foreground services.

I have referred this but it is also not working..

Amin Pinjari
  • 2,129
  • 3
  • 25
  • 53
  • From Nougat the Services will not run in background after few time, it will get stopped by system automatically, Please try JobService with Job Scheduler. – Paresh Rajput Dec 13 '18 at 11:55
  • Possible duplicate of [How to determine if an Android Service is running in the foreground?](https://stackoverflow.com/questions/6452466/how-to-determine-if-an-android-service-is-running-in-the-foreground) – AdamHurwitz Jun 07 '19 at 18:43
  • it is not duplicate @Adam – Amin Pinjari Jun 08 '19 at 09:13
  • They are both asking how to check if a Foreground Service is running @amin. Also, why are you looking to check if the service is running? If it is either to start or create one, then [this solution](https://stackoverflow.com/a/55208488/2253682) will be useful. – AdamHurwitz Jun 08 '19 at 17:41
  • 1
    No this solution is not helpfull in custom OS this never helps. I have tried it on many devices and ended to get weather services is running or not. This is totally different what you r thinking of. @Adam – Amin Pinjari Jun 09 '19 at 04:28
  • @Adam, the code in the case that you mention is depreciated and will no longer work. – MBentley Jul 09 '19 at 14:05

1 Answers1

3

The ActivityManager.getRunningServices() API has been deprecated since API 26 and is no longer available as of Oreo. It was intended to only be used for debugging, not production apps. You should not rely on this type of API for your app.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • 1
    Agree! but is there any way to check whether service is running or not? – Amin Pinjari Dec 13 '18 at 12:05
  • Probably not without a lot of work and would be very difficult to support long term as a 3rd party app. You might be able to get all running processes then work through each to find your package. But, that would not tell you whether or not a service is running. – Larry Schiefer Dec 13 '18 at 12:23
  • 1
    It seems crazy that they would develop this flaky system of setting up services and then not provide some way for developers to check what state they are in. – MBentley Jul 09 '19 at 14:07
  • It is different, but not unworkable. The typical pattern is for your app (e.g. `Activity`) to always start your `Service` and the service knows its own state. If it is already running, it can simply ignore the new start request (or use it to stimulate an update, if needed.) As long as this is not happening constantly, the overhead involved with calling `startService` should be negligible. – Larry Schiefer Jul 10 '19 at 15:18