Friday 7 April 2017

Check wheather Service is running or not

17:25:00 Posted by Kumanan ,
Create a Class in a name of Services :

public class Services {

    public static boolean isMyServiceRunning(Activity mActivity, Class<?> serviceClass) {
        ActivityManager manager = (ActivityManager) mActivity.getSystemService(Context.ACTIVITY_SERVICE);
        for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
            if (serviceClass.getName().equals(service.service.getClassName())) {
                return true;
            }
        }
        return false;
    }
}


In Your Activity :

  if (Services.isMyServiceRunning(ActivityName.this, ServiceName.class)) {
     // Write your condition here if service is running
  }