When I was dealing with the Services in Android, I found out that, we start a service whenever for the first time an application is created and started. But the next time, when we open the same application, then also the Service gets started.
So, I searched for this"How can i check that whether Service is running in Background or not?". And I found a great solution too.
- private boolean isMyServiceRunning() {
- ActivityManager manager = (ActivityManager)getSystemService(ACTIVITY_SERVICE);
- for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
- if (YourService.class.getName().equals(service.service.getClassName())) {
- return true;
- }
- }
- return false;
- }
Here, "
YourService" is the name of the
class of your service. This way you can check whether your service is running in the Background or not.