Androidstudio Implementation Aidl

Source: Internet
Author: User

Aidl Steps to use

Aidl the parameters and return values passed by the remote call support Java basic types (int long Booen char byte, etc.) and string,list,map. Of course, it also supports the delivery of a custom object.

Service side

Create a new Myaidldemoserver project, and then right-click the main directory to create a new Aidl directory, and then create a new Imyaidlinterface.aidl file under the directory with the following code:

  

1 Interface imyaidlinterface {23     int Add (intint  arg2); 4 5 }  //aidl The code in the file does not need to add any modifiers

This defines a imyaidlinterface interface, which defines the Add method for summing calculations.

Then build the current project (make project in the Build option).

Will find in app/build/generated/source/aidl/ The debug directory generates a file with the same package name as the Imyaidlinterface.aidl file, which automatically generates the Imyaidlinterface file, which automatically implements some methods for remote invocation.

Writing Remote Services
The new MyService class inherits the service and implements the following code.

  

1  Public classMyService extends Service {2Imyaidlinterface.stub mstub =Newimyaidlinterface.stub () {3 @Override4          Public intAddintArg1,intarg2) throws RemoteException {5             returnArg1 +arg2;6         }7     };8 9 @OverrideTen      Publicibinder onbind (Intent Intent) { One         returnmstub; A     } -}

The code in the service overrides the Add method in the Imyaidlinterface.stub class and then returns the overridden Imyaidlinterface.stub class by overriding the Onbind () method.

The service is then configured on the Androidmanifest.xml.

1<Service2android:process=": Remote"3Android:name=". MyService"4Android:enabled="true"5Android:exported="true">6<intent-filter>7<action android:name="Co.example.leo.myService"/>8</intent-filter>9</service>

The Android:process property is set and set to ": Remote".

android:process= ": remote", represented in the application, when the service is needed, a new process is automatically created. And if it is android:process= "remote", there is no ":" semicolon, then the global process is created, and the process is shared by different applications.

Then an intent filter was added.

Client
Create a new Myaidldemocustomer project and directly copy the server's Aidl directory directly to the client's main directory. As a result, the client's aidl is not written, directly and the server is exactly the same. Include the package name of the path, and so on. Of course, you can also re-write the Aidl file on the client side.

To edit a layout file:

1<?xml version="1.0"encoding="Utf-8"?>2<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"3Android:layout_width="match_parent"4android:layout_height="match_parent">5 6<TextView7Android:id="@+id/tv"8Android:layout_width="wrap_content"9android:layout_height="wrap_content"TenAndroid:textsize="30SP"/> One</LinearLayout>

Only one textview is used here to show the final calculation results.

Then edit the client's calling code:

1  Public classMainactivity extends Appcompatactivity {2 3 TextView TV;4 imyaidlinterface mstub;5 6 @Override7         protected voidonCreate (Bundle savedinstancestate) {8 super.oncreate (savedinstancestate);9 Setcontentview (r.layout.activity_main);Ten              OneTV =(TextView) Findviewbyid (r.id.tv); A  -Intent Intent =NewIntent (); -             //since the service is implicitly started, the corresponding Action,a is added to the same server as before.  theIntent.setaction ("Co.example.leo.myService"); -             //after Android 5.0, the direct set action cannot start the corresponding service, you need to set PackageName or component.  -Intent.setpackage ("Co.example.leo.myaidldemoserver");//PackageName needs to be consistent with the service side. - Bindservice (intent,serviceconnection,bind_auto_create); +         } -  +         PrivateServiceconnection serviceconnection =Newserviceconnection () { A @Override at              Public voidonserviceconnected (componentname name, IBinder service) { -                 //Call the Asinterface () method to get the Imyaidlinterface instance -Mstub =IMyAidlInterface.Stub.asInterface (service); -                 if(Mstub = =NULL) { -LOG.E ("mainactivity","The mstub is null"); -}Else{//when Mstub is not empty, it calls its Add method for calculation and displays it to TextView.  in                     Try { -                         intValue = Mstub.add (1,8); toTv.settext (value +""); +}Catch(RemoteException e) { - e.printstacktrace (); the                     } *                 } $             }Panax Notoginseng  - @Override the              Public voidonservicedisconnected (componentname name) { +  A             } the         }; +  -  $ @Override $         protected voidOnDestroy () { -             //Unbind Service - Super.ondestroy (); the Unbindservice (serviceconnection); -         }Wuyi}
Finally installed on the client and server, open the client will find that the service-side method has been called and calculated the results.
Summarize

This is one of the simplest aidl programming under as:
1. Create a aidl directory on the server, and a new. Aidl is the suffix of the interface class that defines the interface method for the remote call.
After 2.build compilation, the Aidl remote implementation class is generated under the App/build/generated/source/aidl/debug directory, which is automatically generated as an as.
3. Configure the service's action and process properties under Androidmanifest.xml.
4. Copy the Aidl directory of the server to the corresponding directory of the client, then write the client calling code, as the simple aidl programming is OK.

Androidstudio Implementation Aidl

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.