37

I want to know MyService.java is working or not?

I create this method:

private boolean isServiceAlive(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;
}

it works fine but manager.getRunningServices() is deprecated from API 26. So have we another good solution(method) to get a list of the services that are currently running?

Community
  • 1
  • 1
Makhanov Madiyar
  • 629
  • 1
  • 6
  • 11
  • it seems that you dont need any `isServiceAlive()` method – pskink Aug 05 '17 at 08:01
  • @pskink Why, I need it Just in my code this method is underlined by a line and I'm a perfectionist wanting to remove this line – Makhanov Madiyar Aug 05 '17 at 08:20
  • @pskink when i click the button myservice is started(here push notification with timer) if i clicked button repeatedly or after closing and starting app, my timer works incorrect. This method solved my problem – Makhanov Madiyar Aug 05 '17 at 08:25
  • 2
    so you have problem in your service, not in your activity, see https://groups.google.com/forum/#!topic/android-developers/jEvXMWgbgzE, `We deliberately don't have an API to check whether a service is running because, nearly without fail, when you want to do something like that you end up with race conditions in your code.` - this is what one of android dev team member said – pskink Aug 05 '17 at 08:27
  • 1
    @MakhanovMadiyar Being a perfectionist is besides the point. This function may not work after the next update - so it's best to create a solution for both API >= 26 and API < 26 –  May 15 '18 at 23:50

3 Answers3

19

Despite it doesn't answer your question, I think you still can use this method for your own services:

For backwards compatibility, it will still return the caller's own services.

If you'd just like to remove the deprecation warning, use @SuppressWarnings("deprecation")

geiger
  • 722
  • 7
  • 12
  • 15
    How does this answer the question? This is a band-aid, not a solution. –  May 15 '18 at 23:47
  • 4
    @FoxDonut It seems that's a working solution for Services within the app and OP wanted to check if the Services are running in his own app, thus it should answer his question. – Roman Rozenshtein Oct 22 '18 at 18:35
8

Welcome to Android O

Another Annoying bug of android Oreo, there is just Deprecated annotation and there is no other way to do this. but maybe in the past android developers decide to create alternative function.

For now as Document says continue to use getRunningServices and use @SuppressWarnings("deprecation") above of your function to get rid of warning.

As of Build.VERSION_CODES.O, this method is no longer available to third party applications. For backwards compatibility, it will still return the caller's own services.

Radesh
  • 13,084
  • 4
  • 51
  • 64
3

Here's what you do. If the services you are using are within your own application, just make a singleton that has a boolean variable for started or stopped. When the service starts, make sure to update that variable.

public class SingletonServiceManager {

    public static boolean isMyServiceRunning = false;

}

The above is literally all i use for a small singleton class, later, in some other class, where you want to know if the service is running...

if(SingletonServiceManager.isMyServiceRunning == true){
    // do something
}

You just have to manage where and when this value is updated. For instance, set it to true when you start the service (inside the service). Then, update the boolean to false when you stop the service (from inside the service)

  • 10
    It probably won't work if the service is running in a different process (i.e. multiple instance of that Singleton class are loaded), see https://developer.android.com/guide/topics/manifest/service-element#proc – TWiStErRob May 20 '18 at 21:49
  • 6
    And what if the service is killed? the boolean will still remain true although the service is not running any more – Daniel Bejan Apr 05 '20 at 23:40
  • 1
    You can set it to true in the service's onCreate and then set it to false in the onDestroy. This way you can easily keep track of the running service. – Ahmed Shahid Jul 14 '20 at 05:33