Developing some Android app happen to know whether a service is active or not, for example I needed to know if the service was launched by my apps or not when I open the app, 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.
It’s easy?! ;-)






