The use of linkToDeath and unlinkToDeath in Android development aidl, aidllinktodeath

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.