1.什么是aidl:aidl是 Android Interface definition language的縮寫(xiě),一看就明白,它是一種android內(nèi)部進(jìn)程通信接口的描述語(yǔ)言,通過(guò)它我們可以定義進(jìn)程間的通信接口
icp:interprocess communication :內(nèi)部進(jìn)程通信
2.既然aidl可以定義并實(shí)現(xiàn)進(jìn)程通信,那么我們?cè)趺词褂盟兀课臋n/android-sdk/docs/guide/developing/tools/aidl.html中對(duì)步驟作了詳細(xì)描述:
--1.Create your .aidl file - This file defines an interface (YourInterface.aidl) that defines the methods and fields available to a client.
創(chuàng)建你的aidl文件,我在后面給出了一個(gè)例子,它的aidl文件定義如下:寫(xiě)法跟java代碼類似,但是這里有一點(diǎn)值得注意的就是它可以引用其它aidl文件中定義的接口,但是不能夠引用你的java類文件中定義的接口
--2.Add the .aidl file to your makefile - (the ADT Plugin for Eclipse manages this for you). Android includes the compiler, called AIDL, in the tools/ directory.
編譯你的aidl文件,這個(gè)只要是在eclipse中開(kāi)發(fā),你的adt插件會(huì)像資源文件一樣把a(bǔ)idl文件編譯成java代碼生成在gen文件夾下,不用手動(dòng)去編譯:編譯生成AIDLService.java如我例子中代碼
--3.Implement your interface methods - The AIDL compiler creates an interface in the 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 the methods you declared in your .aidl file.
實(shí)現(xiàn)你定義aidl接口中的內(nèi)部抽象類Stub,public static abstract class Stub extends android.os.Binder implements com.cao.android.demos.binder.aidl.AIDLService
Stub類繼承了Binder,并繼承我們?cè)赼idl文件中定義的接口,我們需要實(shí)現(xiàn)接口方法,下面是我在例子中實(shí)現(xiàn)的Stub類:
Stub翻譯成中文是存根的意思,注意Stub對(duì)象是在被調(diào)用端進(jìn)程,也就是服務(wù)端進(jìn)程,至此,服務(wù)端aidl服務(wù)端得編碼完成了。
--4.Expose your interface to clients - If you're writing a service, you should extend Service and override Service.onBind(Intent) to return an instance of your class that implements your interface.
第四步告訴你怎么在客戶端如何調(diào)用服務(wù)端得aidl描述的接口對(duì)象,doc只告訴我們需要實(shí)現(xiàn)Service.onBind(Intent)方法,該方法會(huì)返回一個(gè)IBinder對(duì)象到客戶端,綁定服務(wù)時(shí)不是需要一個(gè)ServiceConnection對(duì)象么,在沒(méi)有了解aidl用法前一直不知道它是什么作用,其實(shí)他就是用來(lái)在客戶端綁定service時(shí)接收service返回的IBinder對(duì)象的:
mService就是AIDLService對(duì)象,具體可以看我后面提供的示例代碼,需要注意在客戶端需要存一個(gè)服務(wù)端實(shí)現(xiàn)了的aidl接口描述文件,但是客戶端只是使用該aidl接口,不需要實(shí)現(xiàn)它的Stub類,獲取服務(wù)端得aidl對(duì)象后mService = AIDLService.Stub.asInterface(service);,就可以在客戶端使用它了,對(duì)mService對(duì)象方法的調(diào)用不是在客戶端執(zhí)行,而是在服務(wù)端執(zhí)行。
4.aidl中使用java類,需要實(shí)現(xiàn)Parcelable接口,并且在定義類相同包下面對(duì)類進(jìn)行聲明:
上面我定義了Rect1類
之后你就可以在aidl接口中對(duì)該類進(jìn)行使用了
package com.cao.android.demos.binder.aidl;
import com.cao.android.demos.binder.aidl.Rect1;
interface AIDLActivity {
void performAction(in Rect1 rect);
}
注意in/out的說(shuō)明,我這里使用了in表示輸入?yún)?shù),out沒(méi)有試過(guò),為什么使用in/out暫時(shí)沒(méi)有做深入研究。
5.aidl使用完整示例,為了清除說(shuō)明aidl使用,我這里寫(xiě)了一個(gè)例子,例子參考了博客:
http://blog.csdn.net/saintswordsman/archive/2010/01/04/5130947.aspx
作出說(shuō)明
例子實(shí)現(xiàn)了一個(gè)AIDLTestActivity,AIDLTestActivity通過(guò)bindservice綁定一個(gè)服務(wù)AIDLTestService,通過(guò)并獲取AIDLTestActivity的一個(gè)aidl對(duì)象AIDLService,該對(duì)象提供兩個(gè)方法,一個(gè)是registerTestCall注冊(cè)一個(gè)aidl對(duì)象,通過(guò)該方法,AIDLTestActivity把本身實(shí)現(xiàn)的一個(gè)aidl對(duì)象AIDLActivity傳到AIDLTestService,在AIDLTestService通過(guò)操作AIDLActivity這個(gè)aidl遠(yuǎn)端對(duì)象代理,使AIDLTestActivity彈出一個(gè)toast,完整例子見(jiàn)我上傳的資源:
http://download.csdn.net/source/3284820
文章倉(cāng)促而成,有什么疑問(wèn)歡迎大家一起討論。
聯(lián)系客服