Learn Python


Click to Learn Python3

Tuesday, October 18, 2011

Check whether service is running or not in android application

Using the below given code we can check whether a particular service in our application is running or not


Java

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


Here in the if condition we need to give the service file name with the package. So on the checking area you can simply call this function in the if statement like



Java

if(isMyServiceRunning()) {
System.out.println("Service is running");
} else {
System.out.println("Service is not running");
}


Have a nice day