Android Aidl Summary

Source: Internet
Author: User

1. Aidl (Android Interface Definition Language)

2, Aidl for interprocess communication, and with the service side of multiple threads concurrency, if only a single thread can use Messenger, if you do not need the IPC can use binder

3, AIDL Syntax: Basic data types can be applied, List map and other limited application. Static field does not apply.

4. Basic usage of Aidl

First step: Implement the. aidl file

Interface Description File 1 , imported package name 2, if the object object is used, it is required to implement the Parcelable interface, and it needs to import the interface package name + class name, if it is primitive type does not need this step.  3, define the method name.  4. All the. aidl files already need to be passed the interface of the object must be in the service and client of a package com.aidl;import com.aidl.Data; Interface iaidldata{    int  getpid ();        String getName ();        Com.aidl.Data getData ();}

2. Implement the. Aidl interface in the service. The actual implemented interface is an abstract inner Stub of the Iaidldata.java generated automatically in Gen

1, you need to declare the service in the configuration file Androidmanifest.xml file, and add intent-The Action property of the filter node, which can typically use the Aidl package name+file name (because the value is consistent with the client on the server)2, you need to implement the interfaces defined in the Iaidldata.aidl file. The Aidl file is an interface profile that automatically generates a Iaidldata.java interface file with the same name in Gen, which contains an abstract class stub that inherits the Android.os.Binder, implements the Iaidldata interface, What we actually need to implement is the stub abstract class.  Public classAidlservice extends service{ Public voidonCreate () {LOG.D ("Aidl","Aidlservice--------------onCreate"); }     Publicibinder onbind (Intent Intent) {returnMbinder; }    PrivateFinal Iaidldata.stub Mbinder =Newiaidldata.stub () { Public intGetpid () {returnProcess.mypid (); }         PublicString GetName () throws RemoteException {return "go or not go is a problem"; }         PublicData GetData () throws remoteexception {data data=NewData (); Data.id=Process.mypid (); Data.name="go or not go is a problem"; returndata; }    };}

3. Bind service, and get Iaidlservice object

1, establish a connection, use the Action property to locate the attribute of the service actoin that you want to use the class name of the Aidl file+The package name (consistent with the service), which requires the same action property to be set in the service, or the service cannot be found. 2, gets the stub object returned by the service, Miaidldata =IaidlData.Stub.asInterface (Service);p ackage com.clent;import com.aidl.iaidldata;import android.app.Activity; Import Android.content.componentname;import Android.content.intent;import android.content.ServiceConnection; Import Android.os.bundle;import android.os.ibinder;import android.os.remoteexception;import android.util.Log; Import Android.view.View; Public classaidlclientactivity extends activity{iaidldata miaidldata;  Public voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);            Setcontentview (R.layout.main); }            protected voidOnStart () {Super.onstart (); LOG.D ("Aidl","onstart----------Bindservice-----"+iaidldata.class. GetName ()); Intent Intent=NewIntent (Iaidldata.class. GetName ());    Bindservice (Intent, msecondaryconnection, bind_auto_create); }    PrivateServiceconnection msecondaryconnection =Newserviceconnection () { Public voidonserviceconnected (componentname className, IBinder service) {LOG.D ("Aidl","onserviceconnected----------------"); Miaidldata=IaidlData.Stub.asInterface (service); }         Public voidonservicedisconnected (componentname className) {miaidldata=NULL;            }    };  Public voidOnClick (view view) {System. out. println ("onclick---------------:"); if(Miaidldata! =NULL)        {            Try{System. out. println ("Name:"+miaidldata.getname ()); System. out. println ("ID:"+miaidldata.getpid ()); System. out. println ("Data:"+miaidldata.getdata (). ID +"   "+Miaidldata.getdata (). name); }            Catch(RemoteException e) {//TODO auto-generated Catch blockE.printstacktrace (); }                    }    }            protected voidOnDestroy () {Super.ondestroy ();    Unbindservice (msecondaryconnection); }}

4. If you need to pass object objects in the Aidl file, you need to add the corresponding. aidl file

1 , define the object data, and implement Parcelable 2 , add the Data.aidl file, and write the import data in the following way 3 , you need to import com.aidl.Data in a. aidl file that references to Data. 4 , you need to add data.aidl and Data.java files on both the server side and the client and need to be consistent. Data.aidlaidl file: Package com.aidl;parcelable Data;

5, add the corresponding object class, and implement the Parcelable interface

 Public classData implements parcelable{ Public intID;  PublicString name;  Public StaticFinal parcelable.creator<data> Creator =NewParcelable.creator<data>()    {         PublicData Createfromparcel (Parcelinch)        {            return NewData (inch); }         PublicData[] NewArray (intsize) {            return NewData[size];    }    };  PublicData () {}PrivateData (Parcelinch) {Readfromparcel (inch); }         Public voidReadfromparcel (Parcelinch) {ID=inch. ReadInt (); Name=inch. readString (); }     Public intdescribecontents () {return 0; }     Public voidWritetoparcel (Parcel dest,intflags)        {dest.writeint (ID);            Dest.writestring (name); }}

Android Aidl Summary

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.