Use of binders (cross-process--aidl, non-cross-process)

Source: Internet
Author: User
Tags stub

First, Binder class

1. Role: Binder is the medium of communication between client and server (connecting various manager bridges), the client obtains the data provided by the server side through Binder object

(Why do you use binder to provide data, and the server cannot pass it on to client data?) )

Because the server side exposes its data through binder creation interface, it can prevent other hackers to invade, cause the data to be stolen or tampered with, only let other users get the data we want to give them, do not let them take the data from the server themselves.

Second, cross-process use (Implement Client Add book object, server-side Receive object and print)

Step One: Create Book.java class, Book.aidl class, Ibookmanager.aidl class

See: How to Create Aidl in Android Studio

Step Two: Create the service and override the Ibookmanager class

/*Bookservice class*/ Public classBookserviceextendsService {Private Static FinalString TAG = "Bookservice"; PrivateList<book>mbooklist; //How to create a Ibookmanager class created in Aidl@NullablePrivateBinder Mibookmanager =Newibookmanager.stub () {@Override PublicList<book> Getbooksitem ()throwsRemoteException {returnmbooklist; } @Override Public voidAddbook (book book)throwsremoteexception {log.d (TAG,"New book added" +book.tostring ());    }    }; @Override Public voidonCreate () {Super. OnCreate (); Mbooklist=NewArraylist<>(); Mbooklist.add (NewBook (1, "Crazy Android Handout")); Mbooklist.add (NewBook (2, "Android Development Art Quest")); } @Override Publicibinder onbind (Intent Intent) {returnMibookmanager; }}

Don't forget to register in Androidmanifest.xml

<android:name= ". Bookservice "        android:process=": Remote ">      <!--  Service creates a new process-- </service>

Step three: Start the service in the client

 Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Mainactivity"; PrivateIbookmanager Mservice; PrivateList<book>mbooklist; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //You must create a Serviceconnection object, as described in service class usageServiceconnection connection =Newserviceconnection () {@Override Public voidonserviceconnected (componentname name, IBinder service) {//convert the binder from the server to the binder of the Chen clientMservice =IBookManager.Stub.asInterface (service); Try {                   //Get all BooksMbooklist =Mservice.getbooksitem (); LOG.D (Tag,mbooklist.get (0). toString ()); //Join the bookMservice.addbook (NewBook (2, "IOS")); } Catch(RemoteException e) {e.printstacktrace (); }} @Override Public voidonservicedisconnected (componentname name) {}}; //Start ServiceIntent Intent =NewIntent ( This, Bookservice.class);    Bindservice (intent,connection,bind_auto_create); }}    


Question not understood: Will the service process terminate when application is finished? The difference from the binder of the thread

Use of binders (cross-process--aidl, non-cross-process)

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.