-2

In my SystemBroadcastReceiver i want to check weather by BG service is running or not, If it was already running then i will wait it to be completed.

I want both my services should run differently and on main thread only

sri
  • 31
  • 2
  • 1
    see this:http://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android – rafsanahmad007 Jan 11 '17 at 10:20
  • 6
    Possible duplicate of [How to check if a service is running on Android?](http://stackoverflow.com/questions/600207/how-to-check-if-a-service-is-running-on-android) – Pushpendra Jan 11 '17 at 10:23

2 Answers2

0

Use below method to check service is running or not:

 public static boolean isMyServiceRunning(Class<?> serviceClass, Context ctx) {
        final ActivityManager manager = (ActivityManager) ctx.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
Parth Shah
  • 64
  • 3
0
private boolean isMyServiceRunning(Class<?> serviceClass) {
    ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceClass.class.getName().equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

This work fine for my application

Rohith Krishnan
  • 648
  • 8
  • 29