Cdacians

Cdacians
Cdacians

Thursday, 5 January 2012

Check whether service is running or not in android application

Using the below given code we can check whether a particular service in ourapplication 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");
}
Thanks 
akm from cdacians(www.cdacians.com)

No comments:

Post a Comment