How to detect that a client of Binder is killed?

835 Views Asked by At

I am using Binder for communication between two processes. Suppose process A (server) creates a Binder and passes it to process B (client). Process B calls the relevant interfaces on the received IBinder.

I know that B can monitor if process A is killed by using linkToDeatch() on that IBinder. The question I would like to ask is.

  1. Is there any way for A to be notified that process B is killed (except for ping).
  2. How to release the resource related to the Binder? directly assign binder to null?
1

There are 1 best solutions below

0
Allen Shaw On

Process A can implement the onBinderDied() method of the DeathRecipient interface on the IBinder object that was passed to B. When B is killed, the binderDied() method will be called, and can be used by A to release resources and cleanup.

DeathRecipient deathRecipient = new DeathRecipient() {
    @Override
    public void binderDied() {
        Log.d(TAG, "binderDied");
        // process B has died, perform cleanup here
        mBinder.asBinder.unlinkToDeath(this, 0);
    }
};
mBinder.linkToDeath(deathRecipient, 0);