Sunday, January 22, 2012

Finding a service status from an activity


private boolean isMyServiceRunning(String serviceName) {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    for (RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
        if (serviceName.equals(service.service.getClassName())) {
            return true;
        }
    }
    return false;
}

isMyServiceRunning("com.example.MyService") will give you the status of the service.

Binder as IBinder for android

Binder is the base class for the remoteable object. The core part of a lightweight remote call procedure mechanism. This class is an implementation of IBinder that provides the standard support creating a local implementation of such an object


Most developer will not implement directly from this class instead use aidl tool. (I do not know anything about that tool!! :-P). We can however derive directly from Binder to implement our own custom RPC protocol



private final IBinder mbinder = new LocalBinder();
public class LocalBinder extends Binder {
LocalService getService() {
return LocalService.this;
}
}

(or) create a raw Binder object directly to use as a token that can be shared acrosses processes.