前一篇我們從理論上來了解Android的Binder機制,本篇從實戰來深入了解Android的Binder機制。
Binder的使用
Binder依賴于Service,在組件(Activity)中通過bindService(),就可以獲取Service中的Binder對象,實現Service與Activity的通信。
服務分為本地服務和遠程服務,都可以使用Binder。
一:本地服務
在本地服務中使用Binder,只需要兩步:
- 聲明一個Service,重寫onBind(),返回一個繼承Binder的自定義對象;
- 聲明一個ServiceConnection,重寫onServiceConnected(),獲取IBinder對象,然后調用Activity的bindService();
聲明Service:
class ServiceWithBinder : Service() {
private val TAG = "ServiceWithBinder"
class InnerBinder : Binder() {
@Throws(RemoteException::class)
fun multiply(x: Int, y: Int): Int {
return x * y
}
@Throws(RemoteException::class)
fun divide(x: Int, y: Int): Int {
return x / y
}
}
override fun onBind(t: Intent): IBinder? {
Log.e(TAG, "onBind")
return InnerBinder()
}
}
在Activity中綁定服務:
fun myBindService() {
val mServiceConnBinder = object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName) {
Log.e(TAG, "onServiceDisconnected Binder")
binderInterface = null
}
override fun onServiceConnected(name: ComponentName, service: IBinder) {
Log.e(TAG, "onServiceConnected Binder")
binderInterface = service as ServiceWithBinder.InnerBinder
//調用服務的方法
binderInterface?.multiply(3,2)
}
}
val intentBinder = Intent(this, ServiceWithBinder::class.java)
//此時使用bindService開啟服務
bindService(intentBinder, mServiceConnBinder, Context.BIND_AUTO_CREATE)
//銷毀服務
unbindService(mServiceConnBinder)
}
二:遠程服務
在遠程服務中使用Binder,需要三步:創建aidl、聲明Service、調用bindService。
1.創建aidl
aidl是進程間通信接口,需要在客戶端(聲明Service的應用)和服務端(調用Service的應用)同時聲明,并且要完全一致。
首先,在服務端端創建aidl:選中項目目錄->右鍵選中new->然后選中AIDL->填寫文件名(比如:ICalcAIDL)->修改aidl的代碼。
// ICalcAIDL.aidl
package com.pmm.demo.advanced;
// Declare any non-default types here with import statements
interface ICalcAIDL {
int add(int x , int y);
int min(int x , int y );
}
然后,在客戶端創建aidl,步驟同上。客戶端的aidl要與服務端的一模一樣,包括包名、類名、方法。
然后,選中Build->Make Project,編譯整個工程,就會在build/generated/source/aidl/debug目錄下生產一個名為ICalcAIDL的接口。
2.聲明Service
在服務端,聲明一個類(比如:MyStub)繼承ICalcAIDL.Stub(ICalcAIDL的代理類,系統幫我們生成的),然后聲明一個繼承Service的類,在onBind()中返回MyStub對象。
class ServiceWithAIDL : Service() {
private val TAG = "ServiceWithAIDL"
private val mBinder = object : ICalcAIDL.Stub() {
@Throws(RemoteException::class)
override fun add(x: Int, y: Int): Int {
return x + y
}
@Throws(RemoteException::class)
override fun min(x: Int, y: Int): Int {
return x - y
}
}
override fun onBind(t: Intent): IBinder? {
Log.e(TAG, "onBind")
return mBinder
}
}
3.調用bindService
在客戶端,聲明一個匿名內部類ServiceConnection,重寫onServiceConnected(),通過ICalcAIDL.Stub.asInterface(iBinder)獲取服務端的ICalcAIDL對象。
fun myBindService() {
val mServiceConnAIDL = object : ServiceConnection {
override fun onServiceDisconnected(name: ComponentName) {
Log.e(TAG, "onServiceDisconnected AIDL")
mCalcAidl = null
}
override fun onServiceConnected(name: ComponentName, service: IBinder) {
Log.e(TAG, "onServiceConnected AIDL")
mCalcAidl = ICalcAIDL.Stub.asInterface(service)
//調用服務的方法
mCalcAidl?.add(3, 2)
}
}
val intentBinder = Intent(this, ServiceWithAIDL::class.java)
//此時使用bindService開啟服務
bindService(intentBinder, mServiceConnAIDL, Context.BIND_AUTO_CREATE)
//銷毀服務
unbindService(mServiceConnAIDL)
}
AIDL機制
aidl是進程間通信接口,它不是Java的類,更像一種協議。它的作用是讓AS自動生成Binder類,供客戶端調用。
aidl文件編譯后,會生成一個Java接口,分為3層:ICalcAIDL、Stub、Proxy。
package com.pmm.demo.advanced;
public interface ICalcAIDL extends android.os.IInterface {
//具體的Binder類
public static abstract class Stub extends android.os.Binder implements com.pmm.demo.advanced.ICalcAIDL {
·······
//代理類
private static class Proxy implements com.pmm.demo.advanced.ICalcAIDL {
·······
}
}
//加
public int add(int x, int y) throws android.os.RemoteException;
//減
public int min(int x, int y) throws android.os.RemoteException;
}
下面是生成類ICalcAIDL的結構圖
1.ICalcAIDL
ICalcAIDL就是aidl文件中聲明的接口,也就是我們要給客戶端調用的功能。
2.Sub
Stub是一個使用ICalcAIDL裝飾的Binder類,是我們實際傳遞給客戶端的對象。通過Stub,客戶端就可以調用ICalcAIDL的方法。
首先是DESCRIPTOR,默認值是包名+類名,作用是在IBinder中查找ICalcAIDL接口。
private static final java.lang.String DESCRIPTOR = "com.pmm.demo.advanced.ICalcAIDL";
然后是asInterface(),作用是返回Proxy對象,也就是把ICalcAIDL對象返回給客戶端。我們注意到一般obj都是對應服務的指引,然后傳遞給Proxy對象。
public static com.pmm.demo.advanced.ICalcAIDL asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin != null) && (iin instanceof com.pmm.demo.advanced.ICalcAIDL))) {
return ((com.pmm.demo.advanced.ICalcAIDL) iin);
}
//返回Proxy對象
return new com.pmm.demo.advanced.ICalcAIDL.Stub.Proxy(obj);
}
然后是onTransact(),作用是處理客戶端的請求,并將處理結果返回給客戶端。
@Override
public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException {
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
//用來指定DESCRIPTOR,從而確定需要通訊的接口
case INTERFACE_TRANSACTION: {
reply.writeString(descriptor);
return true;
}
//用來處理ICalcAIDL的add()
case TRANSACTION_add: {
//說明data正準備給指定DESCRIPTOR的接口使用
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
int _result = this.add(_arg0, _arg1);
//說明當前操作沒有出現異常
reply.writeNoException();
reply.writeInt(_result);
return true;
}
//用來處理ICalcAIDL的min()
case TRANSACTION_min: {
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
int _result = this.min(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
default: {
return super.onTransact(code, data, reply, flags);
}
}
}
3.Proxy
Proxy,顧名思義,就是ICalcAIDL的代理類,用來實現ICalcAIDL的方法。Proxy的作用是將需要傳遞的參數轉化為Parcel,從而跨進程傳輸。
我們來看下核心代碼,只顯示add(),不顯示min方法。
private static class Proxy implements com.pmm.demo.advanced.ICalcAIDL {
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote) {
mRemote = remote;
}
@Override
public int add(int x, int y) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
//用來標識_data是給含有DESCRIPTOR標志的Binder接口的參數
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(x);
_data.writeInt(y);
//調用Stub的transact,處理add請求
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
//獲取Stub的處理結果
_result = _reply.readInt();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
我們可以看到,前面asInterface(obj)傳遞進來的對象,是我們代理里真正的執行對象。比如:mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
AIDL其實通過我們寫的aidl文件,幫助我們生成了一個接口,一個Stub類用于服務端,一個Proxy類用于客戶端調用。
客戶端使用AIDL接口的asInterface()獲取對應的代理,代理調用對應的方法,方法里都有mRemote.transact()進行具體的調用發消息給服務器,最后服務端對應Binder對象中的onTransact()來處理客戶端的請求,并將處理結果返回給客戶端。
PS:本文
整理
自以下博客
安卓移動架構07-Binder核心機
Android aidl Binder框架淺析
若有發現問題請致郵 caoyanglee92@gmail.com