Check if an Android’s service is running with java api

26 February 2012

Android, Java

When we are developing some Android app, can happen we would to know whether a service is active or not, for example I needed to know if when I open the app, one service was launched by my apps or not, to see the start button or stop button for the service.

Let’s see how it could be a function that will work for us:

private boolean isMyServiceRunning() {
      ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
      for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
          if ("com.example.MyService".equals(service.service.getClassName())) {
               return true;
          }
      }
      return false;
}

In place of the string: com.example.MyService we put the string identifying the service that interests us.

Is it easy?! 😉

Se ti è stato utile il mio articolo, spendi un secondo del tuo tempo e dammi un +1, Google ed io ne saremmo felici 🙂 Grazie mille 🙂
,

No comments yet.

Leave a Reply

*