免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開(kāi)APP
userphoto
未登錄

開(kāi)通VIP,暢享免費(fèi)電子書(shū)等14項(xiàng)超值服

開(kāi)通VIP
Android AIDL使用詳解

  693人閱讀 

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類文件中定義的接口

  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 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類:
 

private final AIDLService.Stub mBinder = new AIDLService.Stub() {  
  1.   
  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.performAction(rect);  
  11.     }  
  12.   
  13.   
  14.     @Override  
  15.     public void registerTestCall(AIDLActivity cb) throws RemoteException {  
  16.         Log("AIDLService.registerTestCall");  
  17.         callback = cb;  
  18.     }  
  19. };  

 

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ì)象的:

  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.     }  
  12.   
  13.   
  14.     public void onServiceDisconnected(ComponentName className) {  
  15.         Log("disconnect service");  
  16.         mService = null;  
  17.     }  
  18. };  

 

 

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)歡迎大家一起討論。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開(kāi)APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
android service中stub作用是什么?
使用AIDL(Android接口描述語(yǔ)言)設(shè)計(jì)和使用遠(yuǎn)程接口
Android使用AIDL設(shè)計(jì)和調(diào)用遠(yuǎn)程接口
camera api2封裝層
使用AIDL實(shí)現(xiàn)跨進(jìn)程雙向通信和傳輸一個(gè)2MB大小的文件
【Android】Service
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服