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文件
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
- package?com.andyidea.aidl;??
- ??
- interface?IBook?{??
- ??????
- ????String?queryBook(int?bookNo);??
- ??
- }??
其中 BookService.java 類中的源碼如下:
[html]?view plaincopy
- package?com.andyidea.service;??
- ??
- import?com.andyidea.aidl.IBook;??
- ??
- import?android.app.Service;??
- import?android.content.Intent;??
- import?android.os.IBinder;??
- import?android.os.RemoteException;??
- ??
- /**??
- ?*?查詢書籍的服務??
- ?*?@author?Andy??
- ?*??
- ?*/??
- public?class?BookService?extends?Service?{??
- ??????
- ????private?String[]?bookNames?=?{"Java編程思想","設計模式","Android開發設計"};???
- ??????
- ????private?IBinder?mIBinder?=?new?BookBinder();??
- ??
- ????@Override??
- ????public?IBinder?onBind(Intent?intent)?{??
- ????????//?TODO?Auto-generated?method?stub??
- ????????return?mIBinder;??
- ????}??
- ??????
- ????/**??
- ?????*?服務中交互的方法??
- ?????*?@param?bookNo??
- ?????*?@return??
- ?????*/??
- ????public?String?queryBookName(int?bookNo){??
- ????????if(bookNo?>?0?&&?bookNo?<=?bookNames.length){??
- ????????????return?bookNames[bookNo-1];??
- ????????}??
- ????????return?null;??
- ????}??
- ??????
- ????private?class?BookBinder?extends?IBook.Stub{??
- ??
- ????????@Override??
- ????????public?String?queryBook(int?bookNo)?throws?RemoteException?{??
- ????????????return?queryBookName(bookNo);??
- ????????}??
- ??????????
- ????}??
- ??
- }??
[html]?view plaincopy
- <?xml?version="1.0"?encoding="utf-8"?>??
- <manifest?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????package="com.andyidea.service"??
- ????android:versionCode="1"??
- ????android:versionName="1.0"?>??
- ??
- ????<uses-sdk?android:minSdkVersion="8"?/>??
- ??
- ????<application??
- ????????android:icon="@drawable/ic_launcher"??
- ????????android:label="@string/app_name"?>??
- ??????????
- <span?style="color:#ff0000;">????????<service?android:name=".BookService">??
- ????????????<intent-filter>??
- ????????????????<action?android:name="com.andyidea.aidl.bookservice"/>??
- ????????????</intent-filter>??
- ????????</service></span>??
- ????</application>??
- ??
- </manifest>??
B應用程序結構圖如下:
我們看到B應用程序也要和服務端同樣的 .aidl 文件,我們可以從A應用程序中把該 aidl 文件中拷貝過來就是了,呵。由于B應用中 .aidl 文件和 A應用中的 .aidl 文件源碼一樣,我在這里就不列出來了。
其中AIDLClientDemoActivity.java源碼如下:【注:其中該客戶端類要通過 bindService 方式來啟動另外一個進程的服務,這樣才能實現和服務進行交互。如果通過startService方式來啟動服務,則不能與服務進行交互】
[html]?view plaincopy
- package?com.andyidea.client;??
- ??
- import?android.app.Activity;??
- import?android.content.ComponentName;??
- import?android.content.Intent;??
- import?android.content.ServiceConnection;??
- import?android.os.Bundle;??
- import?android.os.IBinder;??
- import?android.os.RemoteException;??
- import?android.view.View;??
- import?android.widget.Button;??
- import?android.widget.EditText;??
- import?android.widget.TextView;??
- ??
- import?com.andyidea.aidl.IBook;??
- ??
- public?class?AIDLClientDemoActivity?extends?Activity?{??
- ??????
- ????private?EditText?numberText;??
- ????private?TextView?resultView;??
- ????private?Button?query;??
- ????private?IBook?bookQuery;??
- ????private?BookConnection?bookConn?=?new?BookConnection();??
- ??????
- ????/**?Called?when?the?activity?is?first?created.?*/??
- ????@Override??
- ????public?void?onCreate(Bundle?savedInstanceState)?{??
- ????????super.onCreate(savedInstanceState);??
- ????????setContentView(R.layout.main);??
- ??????????
- ????????numberText?=?(EditText)?this.findViewById(R.id.number);??
- ????????resultView?=?(TextView)?this.findViewById(R.id.resultView);??
- ????????query?=?(Button)findViewById(R.id.query);??
- ??????????
- ????????Intent?service?=?new?Intent("com.andyidea.aidl.bookservice");??
- ????????bindService(service,?bookConn,?BIND_AUTO_CREATE);??
- ??????????
- ????????query.setOnClickListener(new?View.OnClickListener()?{??
- ??????????????
- ????????????@Override??
- ????????????public?void?onClick(View?v)?{??
- ????????????????String?number?=?numberText.getText().toString();??
- ????????????????int?num?=?Integer.valueOf(number);??
- ????????????????try?{??
- ????????????????????resultView.setText(bookQuery.queryBook(num));??
- ????????????????}?catch?(RemoteException?e)?{??
- ????????????????????e.printStackTrace();??
- ????????????????}??
- ????????????}??
- ????????});??
- ????}??
- ??????
- ????@Override??
- ????protected?void?onDestroy()?{??
- ????????unbindService(bookConn);??
- ????????super.onDestroy();??
- ????}??
- ??????
- ????private?final?class?BookConnection?implements?ServiceConnection{??
- ??
- ????????@Override??
- ????????public?void?onServiceConnected(ComponentName?name,?IBinder?service)?{??
- ????????????//?TODO?Auto-generated?method?stub??
- ????????????bookQuery?=?IBook.Stub.asInterface(service);??
- ????????}??
- ??
- ????????@Override??
- ????????public?void?onServiceDisconnected(ComponentName?name)?{??
- ????????????//?TODO?Auto-generated?method?stub??
- ????????????bookQuery?=?null;??
- ????????}??
- ??????????
- ????}??
- }??
[html]?view plaincopy
- <?xml?version="1.0"?encoding="utf-8"?>??
- <LinearLayout?xmlns:android="http://schemas.android.com/apk/res/android"??
- ????android:layout_width="fill_parent"??
- ????android:layout_height="fill_parent"??
- ????android:orientation="vertical"?>??
- ??
- ????<TextView??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"??
- ????????android:text="書籍編號"?/>??
- ????<EditText???
- ????????android:id="@+id/number"??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"/>??
- ????<Button??
- ????????android:id="@+id/query"??
- ????????android:layout_width="wrap_content"??
- ????????android:layout_height="wrap_content"??
- ????????android:text="查詢"/>??
- ????<TextView???
- ????????android:id="@+id/resultView"??
- ????????android:layout_width="fill_parent"??
- ????????android:layout_height="wrap_content"/>??
- </LinearLayout>??
通過上面的截圖,我們輸入書籍編號 1,就可以查詢出相應的書籍名稱,到此,我們就可以了解了 Android應用中如何通過AIDL機制實現兩個進程的通訊。
轉載于:https://www.cnblogs.com/ansionchen/archive/2012/07/03/3019206.html
總結
以上是生活随笔為你收集整理的Android应用中通过AIDL机制实现进程间的通讯实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 《梦仙》第十句是什么
- 下一篇: 现在市场上的白鲢鱼和花鲢鱼多少钱一斤