--aidl Summary of Android

Source: Internet
Author: User

Reprint Please specify source: http://blog.csdn.net/l1028386804/article/details/47071683
    • Aidl (Android Interface Definition Language)
    • Aidl is suitable for interprocess communication, and is concurrent with multiple threads on the service side, if only a single thread can use Messenger, and if the IPC is not required, the binder can be used
    • Aidl syntax: the underlying data types are applicable, and the List map is limited. Static field does not apply.
    • Aidl Basic Usage
1. Implement the. aidl file

Interface description file 1, imported package Name 2, if you use the Object object, you need to implement the Parcelable interface, and you need 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 to 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, the need to declare the service in the configuration file Androidmanifest.xml file, and add the Intent-filter node's Action property, this property can generally use the AIDL package name + file name (because the value in the server and the client consistent) 2,   The interface defined in the Iaidldata.aidl file needs to be implemented. 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 class Aidlservice extends service{public void OnCreate () {log.d ("Aidl", "Aidlservice--------------on    Create ");    } public IBinder Onbind (Intent Intent) {return mbinder; } private Final Iaidldata.stub Mbinder = new Iaidldata.stub () {public int getpid () {retur        n Process.mypid ();        } public String GetName () throws RemoteException {return ' Go or not go ' is a problem ';            } public Data GetData () throws remoteexception {Data data = new data ();            Data.id = Process.mypid ();            Data.name = "Go or not Go is a problem";        return data; }    };}

3. Bind service, and get Iaidlservice object

1. To establish a connection, use the Action property to locate the required service Actoin attribute with the class name of the Aidl file + the package name (consistent with the service), before you need to set the same Action property in the service, or the service cannot be found. 2. Get 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 class AidlClientActivity extends        activity{Iaidldata Miaidldata;        public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);            Setcontentview (R.layout.main);        } protected void OnStart () {Super.onstart ();        LOG.D ("Aidl", "OnStart----------bindservice-----" +iaidldata.class.getname ());        Intent Intent = new Intent (IaidlData.class.getName ());    Bindservice (Intent, msecondaryconnection, bind_auto_create); } privateServiceconnection msecondaryconnection = new Serviceconnection () {public void onserviceconnected (componentname            ClassName, IBinder service) {log.d ("Aidl", "onserviceconnected----------------");        Miaidldata = IaidlData.Stub.asInterface (service);        } public void onservicedisconnected (ComponentName className) {miaidldata = null;            }    };        public void OnClick (view view) {System.out.println ("onclick---------------:"); if (miaidldata! = null) {try {System.out.println ("Name:" +miaidldata.getna                                Me ());                                SYSTEM.OUT.PRINTLN ("ID:" +miaidldata.getpid ());            SYSTEM.OUT.PRINTLN ("Data:" +miaidldata.getdata (). ID + "" +miaidldata.getdata (). name); } catch (RemoteException e) {//TODO auto-generated catch block E.pri NtstacktRace ();        }}} protected void OnDestroy () {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 Parcelable2, add Data.aidl file, and in the following way to write the import Data3, need to refer to the data in the. aidl file import com.aidl.Data4, need on the server and The client adds data.aidl and Data.java files and needs to be consistent. Data.aidlaidl file: Package com.aidl;    Parcelable Data;
5, add the corresponding object class, and implement the Parcelable interface

public class Data implements parcelable{public    int id;    public String name;    public static final parcelable.creator<data> Creator = new Parcelable.creator<data> ()    {        public Data Createfromparcel (Parcel in)        {            return new Data (in);        Public data[] NewArray (int size)        {            return new data[size];        }    ;    Public data ()    {    }    private Data (Parcel in)    {        readfromparcel (in);    }        public void Readfromparcel (Parcel in)    {        id = in.readint ();        Name = In.readstring ();            }    public int describecontents ()    {        return 0;    }    public void Writetoparcel (Parcel dest, int flags)    {        dest.writeint (ID);        Dest.writestring (name);}            }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

--aidl Summary of Android

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.