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

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 🙂
Precedente Vi presento AccadeQui!! Successivo AccadeQui si aggiorna e si aggiornerà...

Leave a Reply

five × 5 =

This site uses Akismet to reduce spam. Learn how your comment data is processed.