Check if service is running or not - Android android.app

Android examples for android.app:ActivityManager

Description

Check if service is running or not

Demo Code

import android.app.ActivityManager;
import android.content.Context;

public class Main {

  /**/* w w  w. java 2  s  . co  m*/
   * Check if service is running or not
   *
   * @param serviceName
   * @param context
   * @return
   */
  public static boolean isServiceRunning(String serviceName, Context context) {
    ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
    for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
      if (serviceName.equals(service.service.getClassName())) {
        return true;
      }
    }
    return false;
  }

}

Related Tutorials