Aidl Android Interface Definition language

Source: Internet
Author: User

android Interface Definition language abbreviated Aidl translated as: Android Interface Definition language

Aidl: interprocess communication. Android Interface defination Language. (The idea of using an interface callback)

Example:

There is a service in the B application that functions as an addition operation.
A application needs to perform the addition function in the B application.
A generates 2 Addend and transmits the data to the B application service. b to perform the operation and return the result of the operation to a.
We're going to define a AIDL interface specification for inter-process communication, and the code format of the Aidl interface is somewhat different from the traditional Java

Aidl usage: How the service is bound.
OnCreate Create
Onbind binding
Onunbind Unbind
OnDestroy Destruction

Story:
Board Brick A (Activity) and board brick s (Service)
1, go to the hardware store to buy glue
2. Apply the glue to the S surface
3. Stick a brick on the s surface (note: A and S have glue in the middle)
4. Check if a and s are glued
5, if stick, take to shoot people

In your code
1. Create Glue Binder
2. Initialize the glue in the Onbind method of the service
3. Use activity to bind service
4. Use an interface to determine if activity and service are connected successfully
5, if the connection is successful, the activity of a program can use the service of program B

Ideas:
The service of the B program is implemented first, with the addition operation function
Then implement the activity of program A to bind the service

Service side:

/*** server, including a service, use to perform addition operation * 1, create Aidl interface used by interprocess communication * 2, implement service * 3, wait for client to access the service*/ Public classMainactivityextendsappcompatactivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);    Setcontentview (R.layout.activity_main); }} Public classPlusserviceextendsService { PublicPlusservice () {}//Create@Override Public voidonCreate () {Super. OnCreate (); LOG.D ("1507", "Service onCreate"); }    //1, Create glue class//Stub : intended as stub. Aidl provides a glue-like class that enables addition operations in this glue    Private Static classMybinderextendsimyaidlinterface.stub {@Override Public intPlusintPLUS01,intPLUS02)throwsRemoteException {returnPlus01 +PLUS02; }    }    //binding@Override Publicibinder onbind (Intent Intent) {LOG.D ("1507", "Service onbind"); return NewMybinder (); }    // Unbind@Override Public BooleanOnunbind (Intent Intent) {LOG.D ("1507", "Service onunbind"); return Super. Onunbind (Intent); }    //destroyed@Override Public voidOnDestroy () {LOG.D ("1507", "Service OnDestroy"); Super. OnDestroy (); }}

Manifest file:

<service            android:name= ". Service. Plusservice "            android:enabled=" true "            android:exported=" true ">            < intent-filter>                <action android:name= "Bind_server"/>            </intent-filter>        </service >

Client:

/*** 3, the service side of the binding server * 4, through the Serviceconnection interface to determine if the connection is successful * 5, if the connection is successful, call service operation*/ Public classMainactivityextendsAppcompatactivityImplementsView.onclicklistener, serviceconnection {//Custom Action     Public Static FinalString action_bind = "Bind_server"; protectedEditText Mplus01et; protectedEditText Mplus02et; protectedButton mcalculatebtn; protectedTextView Mresulttv; PrivateImyaidlinterface Mplusinterface; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); LOG.D ("1507", "Activity onCreate"); Super. Setcontentview (R.layout.activity_main);        Initview ();    Bindservice (); }    //Binding Service    Private voidBindservice () {Intent Intent=NewIntent (Action_bind);//ActionIntent.setpackage ("Net.bwie.aidlserver");//package name corresponding to server side        Booleanisbindsuccessful = Bindservice (Intent, This, context.bind_auto_create); Toast.maketext ( This, "" +isbindsuccessful, Toast.length_short). Show (); } @Override Public voidOnClick (view view) {if(View.getid () = =r.id.calculate_btn) {            intPLUS01 =Integer.parseint (Mplus01et.gettext (). toString ()); intPLUS02 =Integer.parseint (Mplus02et.gettext (). toString ()); //calling methods in the Aidl interface            Try {                intresult =Mplusinterface.plus (plus01, PLUS02); Mresulttv.settext ("The result is:" +result); } Catch(RemoteException e) {e.printstacktrace (); }        }    }

Aidl Android Interface Definition language

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.