Android AIDL使用詳解

來源:互聯網
上載者:User

1.什麼是aidl:aidl是 Android Interface definition language的縮寫,一看就明白,它是一種android內部進程通訊介面的描述語言,通過它我們可以定義進程間的通訊介面
icp:interprocess communication :內部進程通訊

 

2.既然aidl可以定義並實現進程通訊,那麼我們怎麼使用它呢?文檔/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.
建立你的aidl檔案,我在後面給出了一個例子,它的aidl檔案定義如下:寫法跟java代碼類似,但是這裡有一點值得注意的就是它可以引用其它aidl檔案中定義的介面,但是不能夠引用你的java類檔案中定義的介面

[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
includes the compiler, called AIDL, in the tools/ directory.
編譯你的aidl檔案,這個只要是在eclipse中開發,你的adt外掛程式會像資源檔一樣把aidl檔案編譯成java代碼產生在gen檔案夾下,不用手動去編譯:編譯產生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.
實現你定義aidl介面中的內部抽象類別
Stub,public static abstract class Stub extends android.os.Binder
implements com.cao.android.demos.binder.aidl.AIDLService
Stub類繼承了Binder,並繼承我們在aidl檔案中定義的介面,我們需要實現介面方法,下面是我在例子中實現的Stub類:
 

[java:showcolumns] view plaincopy·········10········20········30········40········50········60········70········80········90········100·······110·······120·······130·······140·······150
  1. private final AIDLService.Stub mBinder = new AIDLService.Stub() {  
  2.   
  3.     @Override  
  4.     public void invokCallBack() throws RemoteException {  
  5.         Log("AIDLService.invokCallBack");  
  6.         Rect1 rect = new Rect1();  
  7.         rect.bottom=-1;  
  8.         rect.left=-1;  
  9.         rect.right=1;  
  10.         rect.top=1;  
  11.         callback.performAction(rect);  
  12.     }  
  13.   
  14.   
  15.     @Override  
  16.     public void registerTestCall(AIDLActivity cb) throws RemoteException {  
  17.         Log("AIDLService.registerTestCall");  
  18.         callback = cb;  
  19.     }  
  20. };  

Stub翻譯成中文是存根的意思,注意Stub對象是在被調用端進程,也就是服務端進程,至此,服務端aidl服務端得編碼完成了。

--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.
第四步告訴你怎麼在用戶端如何調用服務端得aidl描述的介面對象,doc只告訴我們需
要實現Service.onBind(Intent)方法,該方法會返回一個IBinder對象到用戶端,綁定服務時不是需要一個
ServiceConnection對象麼,在沒有瞭解aidl用法前一直不知道它是什麼作用,其實他就是用來在用戶端綁定service時接收
service返回的IBinder對象的:

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

 

mService就是AIDLService對象,具體可以看我後面提
供的範例程式碼,需要注意在用戶端需要存一個服務端實現了的aidl介面描述檔案,但是用戶端只是使用該aidl介面,不需要實現它的Stub類,擷取服務
端得aidl對象後mService =
AIDLService.Stub.asInterface(service);,就可以在用戶端使用它了,對mService對象方法的調用不是在客戶
端執行,而是在服務端執行。

4.aidl中使用java類,需要實現Parcelable介面,並且在定義類相同包下面對類進行聲明:

上面我定義了Rect1類
之後你就可以在aidl介面中對該類進行使用了
package com.cao.android.demos.binder.aidl; 
import com.cao.android.demos.binder.aidl.Rect1;
interface AIDLActivity {  
    void performAction(in Rect1 rect);  

注意in/out的說明,我這裡使用了in表示輸入參數,out沒有試過,為什麼使用in/out暫時沒有做深入研究。

5.aidl使用完整樣本,為了清除說明aidl使用,我這裡寫了一個例子,例子參考了部落格:

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

作出說明

例子實現了一個
AIDLTestActivity,AIDLTestActivity通過bindservice綁定一個服務AIDLTestService,通過並獲
取AIDLTestActivity的一個aidl對象AIDLService,該對象提供兩個方法,一個是registerTestCall註冊一個
aidl對象,通過該方法,AIDLTestActivity把本身實現的一個aidl對象AIDLActivity傳到
AIDLTestService,在AIDLTestService通過操作AIDLActivity這個aidl遠端對象代理,使
AIDLTestActivity彈出一個toast,完整例子見我上傳的資源:

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

文章倉促而成,有什麼疑問歡迎大家一起討論。

原文:http://blog.csdn.net/stonecao/article/details/6425019

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.