Android-AIDL Summary

Source: Internet
Author: User

Android-AIDL Summary

 
AIDL (Android Interface Definition Language) AIDL is suitable for communication between processes and concurrent connections with multiple threads on the Service end. If it is only a single thread, you can use Messenger, if you do not need IPC, you can use BinderAIDL Syntax: basic data types are applicable, and List Map is applicable only. Static field is not applicable. Basic usage of AIDL 1. Implement the. aidl File

 

Interface Description file 1. Imported package name 2. If an Object is used, the implement Parcelable interface of this Object is required, and the interface package name + class name must be imported; this step is not required for primitive type. 3. Define the method name. 4. All. the object interface to be passed in the aidl file must have 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 the IaidlData. java Abstract internal class Stub automatically generated in gen.

 

1. In the configuration file Androidmanifest. declare the Service in the xml file and add the action attribute of the intent-filter node. This attribute can generally use the package name + file name of aidl (because the value is consistent with that of the client) 2. Implement IaidlData. the interface defined in the aidl file. The aidl file is an interface description file that automatically generates an IaidlData file with the same name in gen. java interface file, which contains an abstract class stub that inherits android. OS. so what we need to implement is the Stub abstract class. Public class AidlService extends Service {public void onCreate () {Log. d (aidl, aidlService -------------- onCreate);} public IBinder onBind (Intent intent) {return mBinder;} private final IaidlData. stub mBinder = new IaidlData. stub () {public int getPid () {return Process. myPid ();} public String getName () throws RemoteException {return go or not go is a problem;} public Data getData () throws RemoteException {Data data Data = new Data (); data. id = Process. myPid (); data. name = go or not go is a problem; return data ;}};}

 

3. Bind the Service and obtain the IaidlService object

 

1. Establish a connection and use the Action attribute to locate the required Service actoin attributes. Use the class name + package name of the aidl file (consistent with the Service ), you must set the same action attribute in the service before. Otherwise, the service cannot be found. 2. Get the stub object returned by the Service, mIaidlData = IaidlData. stub. asInterface (service); package 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 AidlClien TActivity 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_CREA TE);} private ServiceConnection 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 (oncl Ick ---------------:); 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 block e. printStackTrace () ;}} protected void onDestroy () {super. onDestroy (); unbindService (mSecondaryConnection );}}

 

4. If the aidl file needs to pass the Object, you need to add the corresponding. aidl file.

 

1. Define the object Data and implement Parcelable2. add Data. aidl file, and write and import Data3 in the following way, you need to reference the Data. import com. aidl. data4. You must add Data to both the server and client. aidl and Data. java files must 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 CREATOR = new Parcelable.Creator()    {        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);            }}

 

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.