Cross-process communication can be in aidl language
Here's how to communicate across processes using the Aidl language
The article refers to "design pattern" a book
Demo Structure Reference
The main file classes are: Ibankaidl.aidl
Java file: aidlbankbinder,backactivity (should be bankactivity wrong), Bankservice (inherit from service, Services Class)
Ibankaidl.aidl file The use of Aidl here is required for the package location, so I put the package name out.
PackageFinishdemo.arcturis.binderandserviceandaidl.binder;//Declare any non-default types here with import statementsInterfaceIbankaidl {/*** Demonstrates some basic types that's can use as parameters * and return values in Aidl. */ voidBasictypes (intAnint,LongAlong,BooleanAboolean,floatAfloat,Doubleadouble, String astring); /*** Account Opening *@paramName name of the owner *@paramPassword Password *@returnAccount Opening Information*/string Openaccount (string name,string password); /*** Save money *@paramMoney *@paramAccount *@return */String Savemoney (intmoney,string account); /*** Withdraw Money *@paramMoney *@paramAccount *@paramPassword *@return */String Takemoney (intmoney,string account,string password); /*** Customer *@paramAccount *@paramPassword *@return */string Closeaccount (string account,string password);
The Aidlbankbinder file inherits the stub class from the Ibankaidl, and then overrides and implements the method within the Aidl, which simulates the operation of a bank
Public classAidlbankbinderextendsibankaidl.stub {@Override Public voidBasictypes (intAnint,LongAlong,BooleanAboolean,floatAfloat,DoubleAdouble, String astring)throwsremoteexception {} @Override Publicstring Openaccount (string name, string password) {returnName+ "Successful account opening! The account number is: "+Uuid.randomuuid (). toString (); } @Override PublicString Savemoney (intMoney , String account) { return"Account:" +account + "deposit" + Money + "unit: RMB"; } @Override PublicString Takemoney (intMoney , String account, string password) { return"Account:" +account + "withdrawal" + Money + "unit: RMB"; } @Override Publicstring Closeaccount (string account, string password) {returnAccount + "success of Sales"; }}
Bankservice file Returns a Aidlbankbinder
Public class extends Service { @Nullable @Override public ibinder onbind (Intent Intent) { // single-process notation // return new Bankbinder (); // different process aidl communication notation return New Aidlbankbinder (); }}
Next, use the Backactivity method in the activity
Public classBackactivityextendsAppcompatactivityImplementsView.onclicklistener {//private Bankbinder Mbankbinder; PrivateIbankaidl Mbankbinder; PrivateTextView tvmsg; Privatecontext Context; PrivateServiceconnection conn =Newserviceconnection () {@Override Public voidonserviceconnected (componentname name, IBinder service) {//Same process notation//Mbankbinder = (bankbinder) service; //different process notationMbankbinder =IBankAidl.Stub.asInterface (service); } @Override Public voidonservicedisconnected (componentname name) {}}; @Overrideprotected voidonCreate (@Nullable Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); Context= This; Tvmsg=(TextView) Findviewbyid (r.id.tv_msg); Intent Intent=NewIntent (); Intent.setaction ("Actionname.aidl.bank.BankService"); //There is a problem here, if you directly use intent this intention to open the service, will be reported//Android Service Intent must be explicit means that the service must be shown the call, this is the new rule after 5.0//This createexplicitfromimplicitintent can turn the implicit call into a dominant callIntent intent1 =NewIntent (Createexplicitfromimplicitintent (context,intent)); Bindservice (intent1,conn,bind_auto_create); INITBTN (R.id.btn_aidl_bank_close); INITBTN (R.id.btn_aidl_bank_open); INITBTN (R.id.btn_aidl_bank_save); INITBTN (R.id.btn_aidl_bank_take); } @Overrideprotected voidOnDestroy () {Super. OnDestroy (); Unbindservice (conn); } Private voidINITBTN (intResID) {Button B=(Button) Findviewbyid (ResID); B.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { CaseR.id.btn_aidl_bank_open://This remoteexception is an exception capture that must be added to the usage of Aidl cross-process communication Try{tvmsg.settext (Mbankbinder.openaccount ("Bigass", "123456")); }Catch(RemoteException e) {e.printstacktrace (); } Break; CaseR.id.btn_aidl_bank_save:Try{tvmsg.settext (Mbankbinder.savemoney (8888888, "bigAss123")); }Catch(RemoteException e) {e.printstacktrace (); } Break; CaseR.id.btn_aidl_bank_take:Try{tvmsg.settext (Mbankbinder.takemoney (520, "bigAss123", "123456")); }Catch(RemoteException e) {e.printstacktrace (); } Break; CaseR.id.btn_aidl_bank_close:Try{tvmsg.settext (Mbankbinder.closeaccount ("BigAss123", "123456")); }Catch(RemoteException e) {e.printstacktrace (); } Break; } } /*** * Android L (Lollipop, API) introduced a new problem when trying to invoke implicit intent, * "Java.lang. Illegalargumentexception:service Intent must is explicit "* * If you are using a implicit Intent, and know only 1 target would answer this intent, * This method would help you turn the implicit intent into the explicit form. * * Inspired from So answer:http://stackoverflow.com/a/26318757/1446466 * @paramContext *@paramimplicitintent-the Original Implicit intent *@returnExplicit Intent created from the implicit original Intent*/ Public StaticIntent createexplicitfromimplicitintent (context context, Intent implicitintent) {//Retrieve all services that can match the given intentPackagemanager pm =Context.getpackagemanager (); List<ResolveInfo> ResolveInfo = pm.queryintentservices (implicitintent, 0); //Make sure-one match was found if(ResolveInfo = =NULL|| Resolveinfo.size ()! = 1) { return NULL; } //Get Component Info and create ComponentNameResolveInfo serviceinfo = resolveinfo.get (0); String PackageName=ServiceInfo.serviceInfo.packageName; String ClassName=ServiceInfo.serviceInfo.name; ComponentName Component=Newcomponentname (PackageName, className); //Create a new intent. use the old one for extras and such reuseIntent explicitintent =NewIntent (implicitintent); //Set the component to be explicitexplicitintent.setcomponent (component); returnexplicitintent; }}
The main idea is to start a service with an implicit method, and then call the Aidl instance class method, the specific Aidl language after Buildapp in
Here you can see inside is the conversion code that he made to achieve inter-process data transfer, interested to see
Android Aidl Language Usage