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
- Package com. Cao. Android. demos. Binder. aidl;
- Import com. Cao. Android. demos. Binder. aidl. aidlactivity;
- Interface aidlservice {
- Void registertestcall (aidlactivity CB );
- Void invokcallback ();
- }
-- 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
- Private Final aidlservice. Stub mbinder = new aidlservice. Stub (){
- @ Override
- Public void invokcallback () throws RemoteException {
- Log ("aidlservice. invokcallback ");
- Rect1 rect = new rect1 ();
- Rect. Bottom =-1;
- Rect. Left =-1;
- Rect. Right = 1;
- Rect. Top = 1;
- Callback. Reverse maction (rect );
- }
- @ Override
- Public void registertestcall (aidlactivity CB) throws RemoteException {
- Log ("aidlservice. registertestcall ");
- Callback = CB;
- }
- };
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
- Aidlservice mservice;
- Private serviceconnection mconnection = new serviceconnection (){
- Public void onserviceconnected (componentname classname, ibinder Service ){
- Log ("Connect service ");
- Mservice = aidlservice. stub. asinterface (service );
- Try {
- Mservice. registertestcall (mcallback );
- } Catch (RemoteException e ){
- }
- }
- Public void onservicedisconnected (componentname classname ){
- Log ("disconnect service ");
- Mservice = NULL;
- }
- };
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