8

I'll like to ask if it's possible to check a service is running from Broadcast receiver.

I know that it's possible in Activity.

Thanks for your precious help

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
user3274646
  • 1,901
  • 2
  • 13
  • 7

1 Answers1

9

yes' it's possible to detect if service is running from anywhere you have available Context object:

private boolean isMyServiceRunning(Context context) {
ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if (MyService.class.getName().equals(service.service.getClassName())) {
        return true;
    }
}

return false;
}

MyService is your service class.

Tal Kanel
  • 10,475
  • 10
  • 60
  • 98