The use of linkToDeath and unlinkToDeath in Android development aidl, aidllinktodeath
1. The Binder death proxy section first introduces two important methods of the Binder class: linkToDeath and unlinkToDeath. We know that the Binder is running in the service process. If the server process is "dead" For some reason, the Binder object will also go with it because the Binder object is hosted in the server process, at this time, our remote call will fail, and the functions of the client process will also be affected. The Binder class provides the linkToDeath method to set the death proxy on the client. When the Binder object on the server is "dead", the client can be notified of death. In this case, we can restore the link.
The client code is as follows.
Public class MainActivity extends Activity {private IAidlCall mIAidlCall; private IBinder. deathRecipient mDeathRecipient = new IBinder. deathRecipient () {@ Override public void binderDied () {// TODO Auto-generated method stub if (mIAidlCall = null) return; mIAidlCall. asBinder (). unlinkToDeath (mDeathRecipient, 0); mIAidlCall = null; // TODO: rebind the remote service.
BindService (new Intent ("demo. action. aidl. IAidlCall ").
SetPackage ("com. example. severdemo"), conn, BIND_AUTO_CREATE );
}; Private ServiceConnection conn = new ServiceConnection () {@ Override public void publish (ComponentName name) {}@ Override public void onServiceConnected (ComponentName, IBinder service) {mIAidlCall = IAidlCall. stub. asInterface (service); try {service. linkToDeath (mDeathRecipient, 0); Toast. makeText (getApplicationContext (), mIAidlCall. getName (), Toast. LENGTH_LONG ). show ();} catch (RemoteException e) {// TODO Auto-generated catch block e. printStackTrace () ;}};@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // "demo. action. aidl. IAidlCall "is the action bindService (new Intent (" demo. action. aidl. IAidlCall ")
. SetPackage ("com. example. severdemo"), conn, BIND_AUTO_CREATE );
} }
Reference: http://blog.csdn.net/chan1116/article/details/49930539