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)