Wednesday, February 8, 2012

How to check if service is running?

There’re situations where you need to check if service is already running. And it’s pretty easy…
private boolean isServiceRunning() {
  ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
  for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
    if (“your.servicename”.equals(service.service.getClassName())) {
      return true;
    }
  }
  return false;
}

5 comments: