Android developer Guide (6) -- AIDL

Source: Internet
Author: User

Preface

This chapter is the developer Guide (Dev Guide)/Developing/Tools/aidl, version Android2.3 r1, translated from "Mobile cloud _ Wenbin", welcome to its blog: "http://blog.csdn.net/caowenbin ", thanks again for "Mobile cloud _ Wenbin "! We look forward to your participation in translation of Android related information, contact me over140@gmail.com.

Original
Http://developer.android.com/guide/developing/tools/aidl.html (Note: 3.0 r1 move below Appendix)
 

Body
Design a Remote Interface Using AIDL)

Because each application runs in its own process space and can run another service process from the application UI, objects are often transferred between different processes. On the Android platform, a process generally cannot access the memory space of another process. Therefore, to perform a conversation, you need to break down the object into basic units that the operating system can understand and orderly pass through the process boundary.

It is tedious to implement this data transmission process through code. Android provides the AIDL tool to handle this task.

AIDL (Android Interface Definition Language) is an IDL Language used to generate code for inter-process communication (IPC) between two processes on the Android device. If you want to call operations on objects in another process (such as Service) in a process (such as Activity), you can use AIDL to generate serializable parameters.

The aidl ipc Mechanism is interface-oriented, like COM or Corba, but more lightweight. It uses a proxy class to transmit data on the client and the implementation end.

 

Use AIDL to implement IPC (Implementing IPC Using AIDL)

To use AIDL to implement the IPC service, follow these steps:

1. Create the. aidl file. This file (YourInterface. aidl) defines available methods and data interfaces for the client.

2. Add the. aidl file to the makefile file (the ADT plug-in Eclipse provides the management function). Android includes the AIDL compiler, which is located in the tools/folder.

3. implementation interface-The AIDL compiler uses the Java language to create an interface from the AIDL interface file. This interface has an inherited internal abstract class named Stub (and implements some additional methods for IPC calls ), all you need to do is to create one that inherits from YourInterface. stub class is implemented in. methods declared in the aidl file.

4. Open an interface to the client-if you are writing a Service, you should inherit the Service and reload Service. onBind (Intent) to return the object instance that implements the interface.

 

Create an. aidl File)

AIDL uses a simple syntax to declare an interface and describe the parameters and return values of its methods and methods. These parameters and return values can be of any type, or even interfaces generated by other AIDL. It is important to import all non-built-in types, even if these types are in the same package as the interface. The following data types supported by AIDL:

* Main Java programming language types (int, boolean, etc.)-The import Statement is not required.

* The following classes (the import Statement is not required ):

String

List-all elements in the List must be the types listed here, including the interfaces and packable types generated by other AIDL. List can be used as a general class (such as List <String>). The specific class received by the other side is generally an ArrayList. These methods use the List interface.

All the elements in Map-Map must be the types listed here, including the interfaces and packable types generated by other AIDL. General maps (such as Map <String, Integer>) are not supported. The specific class received by the other side is generally a HashMap. These methods use the Map interface.

CharSequence-This class is the character sequence used by TextView and other control objects.

* The import Statement declaration is required for APIS generated by other AIDL passed in reference mode.

* The Parcelable protocol and custom classes passed by value must be declared using the import Statement.

The basic AIDL syntax is as follows:

  

 

Implementing the Interface)

AIDL and. if you use the Eclipse plug-in, aidl runs automatically as part of the compilation process (you do not need to run AIDL before compiling the Project). If there is no plug-in, you need to run AIDL first.

The generated interface contains an abstract internal class named Stub, which declares all. the methods described in aidl, Stub also defines a small number of auxiliary methods, especially asInterface (), through which IBinder (when applicationContext. when bindService () is successfully called, it is passed to the onServiceConnected () of the client and an interface instance used to call the IPC Method is returned. For more details, see Calling an IPC Method.

To implement your own interface, start with YourInterface. stub class inheritance, and then implement the relevant methods (you can create. the aidl file then implements the stub method instead of intermediate compilation. java file processing. aidl file ).

This example calls the IRemoteService interface. Here an anonymous object is used and only one getPid () interface is used.

  
Here are several descriptions of the implementation interface:

* No exceptions are returned to the caller.

* By default, IPC calls are synchronized. If it is known that the IPC server will take many milliseconds to complete, do not call it in the Activity or View thread, otherwise, the application will be suspended (Android may display the "Application not responding" dialog box). You can try to call it in an independent thread.

* The AIDL interface only supports methods and does not declare static members.

 

Expose the Interface (Exposing Your Interface to Clients) to the client)

After implementing the interface, you need to expose the interface to the client, that is, publish the Service. The implementation method is to inherit the Service and then implement the Service. onBind (Intent) returns a class object that implements the interface. The following code snippet shows how to expose the IRemoteService interface to the client.

Public class RemoteService extends Service {
...
@ Override
Public IBinder onBind (Intent intent ){
// Select the interface to return. If your service only implements
// A single interface, you can just return it here without checking
// The Intent.
If (IRemoteService. class. getName (). equals (intent. getAction ())){
Return mBinder;
}
If (ISecondary. class. getName (). equals (intent. getAction ())){
Return mSecondaryBinder;
}
Return null;
}

/**
* The IRemoteInterface is defined through IDL
*/
Private final IRemoteService. Stub mBinder = new IRemoteService. Stub (){
Public void registerCallback (IRemoteServiceCallback cb ){
If (cb! = Null) mCallbacks. register (cb );
}
Public void unregisterCallback (IRemoteServiceCallback cb ){
If (cb! = Null) mCallbacks. unregister (cb );
}
};

/**
* A secondary interface to the service.
*/
Private final ISecondary. Stub mSecondaryBinder = new ISecondary. Stub (){
Public int getPid (){
Return Process. myPid ();
}
Public void basicTypes (int anInt, long aLong, boolean sort lean,
Float aFloat, double aDouble, String aString ){
}
};

}
  

Pass the Parameter Pass by value Parameters using Parcelables using the package Interface

This idea can be implemented if a class can pass AIDL between processes. It must be ensured that this class has

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.