Check if Android service is currently running

Sometimes we need to know if our android service is running. I always recommend binding to the service and listening for the ServiceDisconnected. However, if we need to know if a service is running from a broadcast receiver, this is how:


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;
}