Android應用中通過AIDL機制實現進程間的通訊實例

       在Android中,每個應用程序都有自己的進程,當需要在不同的進程之間傳遞對象時,該如何實現呢?顯然,Java中是不支持跨進程內存共享的,因此要傳遞對象,需要把對象解析成操作系統能夠理解的數據格式,以達到跨界對象訪問的目的。在Android中,則採用AIDL(Android Interface Definition Language :接口定義語言)方式實現。

AIDL (Android Interface Definition Language)是一種IDL 語言,用於生成可以在Android設備上兩個進程之間進行進程間通信(IPC)的代碼。如果在一個進程中(例如Activity)要調用另一個進程中(例如Service)對象的操作,就可以使用AIDL生成可序列化的參數。

AIDL IPC機制是面向接口的,它是使用代理類在客戶端和實現端傳遞數據。

使用AIDL實現IPC

使用AIDL實現IPC服務的步驟是:

1. 創建.aidl文件-該文件(YourInterface.aidl)定義了客戶端可用的方法和數據的接口。

2. makefile文件中加入.aidl文件-Eclipse中的ADT插件提供管理功能)Android包括名爲AIDL的編譯器,位於tools/文件夾。

3. 實現接口-AIDL編譯器從AIDL接口文件中利用Java語言創建接口,該接口有一個繼承的命名爲Stub的內部抽象類(並且實現了一些IPC調用的附加方法),要做的就是創建一個繼承於YourInterface.Stub的類並且實現在.aidl文件中聲明的方法。

4. 向客戶端公開接口-如果是編寫服務,應該繼承Service並且重載Service.onBind(Intent) 以返回實現了接口的對象實例

創建.aidl文件

AIDL使用簡單的語法來聲明接口,描述其方法以及方法的參數和返回值。這些參數和返回值可以是任何類型,甚至是其他AIDL生成的接口。重要的是必須導入所有非內置類型,哪怕是這些類型是在與接口相同的包中。下面是AIDL能支持的數據類型:

1.Java編程語言的主要類型 (int, boolean等) — 不需要 import 語句。

2.以下的類 (不需要import 語句):

String

List -列表中的所有元素必須是在此列出的類型,包括其他AIDL生成的接口和可打包類型。List可以像一般的類(例如List<String>)那樣使用,另一邊接收的具體類一般是一個ArrayList,這些方法會使用List接口。

Map - Map中的所有元素必須是在此列出的類型,包括其他AIDL生成的接口和可打包類型。一般的maps(例如Map<String,Integer>)不被支持,另一邊接收的具體類一般是一個HashMap,這些方法會使用Map接口。

CharSequence -該類是被TextView和其他控件對象使用的字符序列。

3.通常引引用方式傳遞的其他AIDL生成的接口,必須要import 語句聲明

4.實現了Parcelable protocol 以及按值傳遞的自定義類,必須要import 語句聲明。

通過對上面的基本瞭解,下面我就以一個具體的實例來說明Android中如何通過AIDL機制來實現兩個進程中實現通訊:(情景假設:例如A應用通過服務Service方式向B應用提供通過書籍編號來查詢書籍名稱的服務)

A應用程序結構圖如下:


通過上面的結構圖可以看到,在A應用程序中創建一個 aidl 的接口,然後系統在 gen 目錄下自動生成相應的 java 文件。

其中 IBook.aidl 文件的源碼:

[html] view plaincopy
  1. package com.andyidea.aidl;  
  2.   
  3. interface IBook {  
  4.       
  5.     String queryBook(int bookNo);  
  6.   
  7. }  

其中 BookService.java 類中的源碼如下:

[html] view plaincopy
  1. package com.andyidea.service;  
  2.   
  3. import com.andyidea.aidl.IBook;  
  4.   
  5. import android.app.Service;  
  6. import android.content.Intent;  
  7. import android.os.IBinder;  
  8. import android.os.RemoteException;  
  9.   
  10. /**  
  11.  * 查詢書籍的服務  
  12.  * @author Andy  
  13.  *  
  14.  */  
  15. public class BookService extends Service {  
  16.       
  17.     private String[] bookNames = {"Java編程思想","設計模式","Android開發設計"};   
  18.       
  19.     private IBinder mIBinder = new BookBinder();  
  20.   
  21.     @Override  
  22.     public IBinder onBind(Intent intent) {  
  23.         // TODO Auto-generated method stub  
  24.         return mIBinder;  
  25.     }  
  26.       
  27.     /**  
  28.      * 服務中交互的方法  
  29.      * @param bookNo  
  30.      * @return  
  31.      */  
  32.     public String queryBookName(int bookNo){  
  33.         if(bookNo > 0 && bookNo <= bookNames.length){  
  34.             return bookNames[bookNo-1];  
  35.         }  
  36.         return null;  
  37.     }  
  38.       
  39.     private class BookBinder extends IBook.Stub{  
  40.   
  41.         @Override  
  42.         public String queryBook(int bookNo) throws RemoteException {  
  43.             return queryBookName(bookNo);  
  44.         }  
  45.           
  46.     }  
  47.   
  48. }  
同時別忘了在 Manifest.xml中配置該服務對象(標紅色的部分),建議採用隱式方式激活該服務,適合不同的進程的意圖。

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.andyidea.service"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk android:minSdkVersion="8" />  
  8.   
  9.     <application  
  10.         android:icon="@drawable/ic_launcher"  
  11.         android:label="@string/app_name" >  
  12.           
  13. <span style="color:#ff0000;">        <service android:name=".BookService">  
  14.             <intent-filter>  
  15.                 <action android:name="com.andyidea.aidl.bookservice"/>  
  16.             </intent-filter>  
  17.         </service></span>  
  18.     </application>  
  19.   
  20. </manifest>  
以上我們已經實現了A應用程序提供服務的功能,下面我們來實現B應用(或者其它需要用到A應用提供服務的應用程序)

B應用程序結構圖如下:


我們看到B應用程序也要和服務端同樣的 .aidl 文件,我們可以從A應用程序中把該 aidl 文件中拷貝過來就是了,呵。由於B應用中 .aidl 文件和 A應用中的 .aidl 文件源碼一樣,我在這裏就不列出來了。

其中AIDLClientDemoActivity.java源碼如下:【注:其中該客戶端類要通過 bindService 方式來啓動另外一個進程的服務,這樣才能實現和服務進行交互。如果通過startService方式來啓動服務,則不能與服務進行交互】

[html] view plaincopy
  1. package com.andyidea.client;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.ComponentName;  
  5. import android.content.Intent;  
  6. import android.content.ServiceConnection;  
  7. import android.os.Bundle;  
  8. import android.os.IBinder;  
  9. import android.os.RemoteException;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13. import android.widget.TextView;  
  14.   
  15. import com.andyidea.aidl.IBook;  
  16.   
  17. public class AIDLClientDemoActivity extends Activity {  
  18.       
  19.     private EditText numberText;  
  20.     private TextView resultView;  
  21.     private Button query;  
  22.     private IBook bookQuery;  
  23.     private BookConnection bookConn = new BookConnection();  
  24.       
  25.     /** Called when the activity is first created. */  
  26.     @Override  
  27.     public void onCreate(Bundle savedInstanceState) {  
  28.         super.onCreate(savedInstanceState);  
  29.         setContentView(R.layout.main);  
  30.           
  31.         numberText = (EditText) this.findViewById(R.id.number);  
  32.         resultView = (TextView) this.findViewById(R.id.resultView);  
  33.         query = (Button)findViewById(R.id.query);  
  34.           
  35.         Intent service = new Intent("com.andyidea.aidl.bookservice");  
  36.         bindService(service, bookConn, BIND_AUTO_CREATE);  
  37.           
  38.         query.setOnClickListener(new View.OnClickListener() {  
  39.               
  40.             @Override  
  41.             public void onClick(View v) {  
  42.                 String number = numberText.getText().toString();  
  43.                 int num = Integer.valueOf(number);  
  44.                 try {  
  45.                     resultView.setText(bookQuery.queryBook(num));  
  46.                 } catch (RemoteException e) {  
  47.                     e.printStackTrace();  
  48.                 }  
  49.             }  
  50.         });  
  51.     }  
  52.       
  53.     @Override  
  54.     protected void onDestroy() {  
  55.         unbindService(bookConn);  
  56.         super.onDestroy();  
  57.     }  
  58.       
  59.     private final class BookConnection implements ServiceConnection{  
  60.   
  61.         @Override  
  62.         public void onServiceConnected(ComponentName name, IBinder service) {  
  63.             // TODO Auto-generated method stub  
  64.             bookQuery = IBook.Stub.asInterface(service);  
  65.         }  
  66.   
  67.         @Override  
  68.         public void onServiceDisconnected(ComponentName name) {  
  69.             // TODO Auto-generated method stub  
  70.             bookQuery = null;  
  71.         }  
  72.           
  73.     }  
  74. }  
其中界面佈局文件 main.xml 源碼:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="書籍編號" />  
  11.     <EditText   
  12.         android:id="@+id/number"  
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"/>  
  15.     <Button  
  16.         android:id="@+id/query"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:text="查詢"/>  
  20.     <TextView   
  21.         android:id="@+id/resultView"  
  22.         android:layout_width="fill_parent"  
  23.         android:layout_height="wrap_content"/>  
  24. </LinearLayout>  
至此,我們已經完成了B應用程序的代碼實現部分,我們要先把A應用程序部署到機器上,然後我們再運行B應用程序。下面我們通過截圖來看下程序運行的結果:

通過上面的截圖,我們輸入書籍編號 1,就可以查詢出相應的書籍名稱,到此,我們就可以瞭解了 Android應用中如何通過AIDL機制實現兩個進程的通訊。

注:本文爲 Andy.Chen 原創,歡迎大家轉載,轉載請大家註明出處,謝謝。

發佈了35 篇原創文章 · 獲贊 20 · 訪問量 18萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章