December 25, 2016

Android How to Check Service is Running or Not

By Lalit Vasan


In some cases you might want to check that a service is running or not. An example user case would be, in your app there is a service that sync data to server and you have given user the possibility to toggle the related service on and off. So how do you know that the service is currently running or not? To Know service running status in your app won’t take more than two steps.

Firstly define below method to know the Service running status. This method return True if service is running.

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

Then Call the method by passing the service class name.

boolean serviceRunningStatus = isServiceRunning(AnyService.class);

Enjoy Coding and Share Knowledge