Android aidl Usage Details

Source: Internet
Author: User

1. what is aidl? aidl is the abbreviation of Android interface definition language. It can be understood at a glance that it is a description language of the internal process Communication Interface of Android, through this interface, we can define the communication interfaces between processes.
ICP: Interprocess Communication: Internal Process Communication

 

2. Since aidl can define and implement process communication, how can we use it? Detailed steps are described in/Android-SDK/docs/GUIDE/developing/tools/aidl.html:

-- 1. Create Your. aidl file-this
File defines an interface (yourinterface. aidl) that defines the methods
And fields available to a client.
Create your aidl file. I will give an example later. Its aidl file is defined as follows: similar to Java code, however, it is worth noting that it can reference interfaces defined in other aidl files, but it cannot reference interfaces defined in your Java class files.

[Java]View plaincopy
  1. Package com. Cao. Android. demos. Binder. aidl;
  2. Import com. Cao. Android. demos. Binder. aidl. aidlactivity;
  3. Interface aidlservice {
  4. Void registertestcall (aidlactivity CB );
  5. Void invokcallback ();
  6. }

-- 2.add The. aidl file to your
Makefile-(the ADT plugin for eclipse manages this for you). Android
Provided des the compiler, called aidl, In the tools/directory.
Compile your aidl file. As long as it is developed in eclipse, your ADT plug-in will compile the aidl file into Java code and generate it in the gen folder like a resource file, you do not need to compile manually: compile and generate aidlservice. java code in my example


-- 3. Implement
Your interface methods-the aidl compiler creates an interface in
Java programming language from your aidl interface. This interface has
An inner abstract class named stub that inherits the interface (and
Implements a few additional methods necessary for the IPC call). You
Must create a class that extends yourinterface. stub and implements
Methods you declared in your. aidl file.
Implement the internal abstract class defined in the aidl Interface
Stub, public static abstract class stub extends Android. OS. Binder
Implements com. Cao. Android. demos. Binder. aidl. aidlservice
The stub class inherits the binder and the interfaces defined in the aidl file. We need to implement the interface methods. below is the stub class I implemented in the example:

[Java: showcolumns]View plaincopy · ····· · 90 ··· · 140 · 150
  1. Private Final aidlservice. Stub mbinder = new aidlservice. Stub (){
  2. @ Override
  3. Public void invokcallback () throws RemoteException {
  4. Log ("aidlservice. invokcallback ");
  5. Rect1 rect = new rect1 ();
  6. Rect. Bottom =-1;
  7. Rect. Left =-1;
  8. Rect. Right = 1;
  9. Rect. Top = 1;
  10. Callback. Reverse maction (rect );
  11. }
  12. @ Override
  13. Public void registertestcall (aidlactivity CB) throws RemoteException {
  14. Log ("aidlservice. registertestcall ");
  15. Callback = CB;
  16. }
  17. };

Stub translates it into Chinese as a stub. Note that the stub object is in the called process, that is, the server process. At this point, the aidl server of the server has completed encoding.

-- 4. Expose your interface to clients-
If you're writing a service, you shoshould extend service and override
Service. onbind (intent) to return an instance of your class that
Implements your interface.
Step 4 shows you how to call the interface object described by aidl on the server side on the client side. Doc only tells us how
To implement the service. onbind (intent) method, this method returns an ibinder object to the client.
Serviceconnection object? I never know what it is before I understand aidl usage. In fact, it is used to receive
The ibinder object returned by the Service:

[Java]View plaincopy
  1. Aidlservice mservice;
  2. Private serviceconnection mconnection = new serviceconnection (){
  3. Public void onserviceconnected (componentname classname, ibinder Service ){
  4. Log ("Connect service ");
  5. Mservice = aidlservice. stub. asinterface (service );
  6. Try {
  7. Mservice. registertestcall (mcallback );
  8. } Catch (RemoteException e ){
  9. }
  10. }
  11. Public void onservicedisconnected (componentname classname ){
  12. Log ("disconnect service ");
  13. Mservice = NULL;
  14. }
  15. };

 

Mservice is the aidlservice object.
For the sample code, you must note that the client needs to store a description file of the aidl interface implemented by the server, but the client only uses this aidl interface and does not need to implement its stub class to obtain services.
After the aidl object is obtained, mservice =
Aidlservice. stub. asinterface (service);, you can use it on the client.
Is executed on the server.

4. To use Java classes in aidl, you must implement the parcelable interface and declare the classes under the same defined class package:

I have defined the rect1 class above.
Then you can use this class in the aidl interface.
Package com. Cao. Android. demos. Binder. aidl;
Import com. Cao. Android. demos. Binder. aidl. rect1;
Interface aidlactivity {
Void upload maction (in rect1 rect );
}
Note the description of in/out. Here I use in to indicate the input parameters. I have not tried out. Why is in/out not used for further research.

5. Complete example of aidl usage. To clear the instructions for aidl usage, I wrote an example here. For example, refer to the blog:

Http://blog.csdn.net/saintswordsman/archive/2010/01/04/5130947.aspx

Instructions

In this example
Aidltestactivity and aidltestactivity bind a service aidltestservice through bindservice.
Aidlservice, an aidl object of aidltestactivity. This object provides two methods. One is registertestcall to register one.
Aidl object. In this method, aidltestactivity uploads an aidl object aidlactivity implemented by itself
Aidltestservice, In the aidltestservice, by operating the aidlactivity remote aidl object proxy
A toast is displayed in aidltestactivity. For the complete example, see the resource I uploaded:

Http://download.csdn.net/source/3284820

The article has been written in a rush. If you have any questions, please join us.

Original article: http://blog.csdn.net/stonecao/article/details/6425019

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.