您好,登錄后才能下訂單哦!
在上一篇文章(Service使用方式)中,介紹了Android進(jìn)程間通信(IPC)的使用,并給出了一個示例。但并沒有深入分析aidl是怎樣可以做到進(jìn)程間通信的,它的執(zhí)行過程是怎樣的?
這篇文章來分析IRemoteService.aidl的執(zhí)行過程,并理解aidl是怎樣跨進(jìn)程通信的。
當(dāng)我們創(chuàng)建IRemoteService.aidl文件時,IDE會為我們在gen目錄中創(chuàng)建相應(yīng)的文件。
[java] view plaincopy
/** This file is auto-generated. DO NOT MODIFY.
* Original file: F:\\workspace\\AndroidImprove\\src\\com\\example\\aidl\\IRemoteService.aidl
*/
package com.example.aidl;
public interface IRemoteService extends android.os.IInterface
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.example.aidl.IRemoteService
{
private static final java.lang.String DESCRIPTOR = "com.example.aidl.IRemoteService";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.example.aidl.IRemoteService interface,
* generating a proxy if needed.
*/
public static com.example.aidl.IRemoteService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.example.aidl.IRemoteService))) {
return ((com.example.aidl.IRemoteService)iin);
}
return new com.example.aidl.IRemoteService.Stub.Proxy(obj);
}
public android.os.IBinder asBinder()
{
return this;
}
@Override public boolean onTransact(int code, android.os.Parcel data, android.os.Parcel reply, int flags) throws android.os.RemoteException
{
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_register:
{
data.enforceInterface(DESCRIPTOR);
com.example.aidl.IRemoteCallback _arg0;
_arg0 = com.example.aidl.IRemoteCallback.Stub.asInterface(data.readStrongBinder());
this.register(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_unregister:
{
data.enforceInterface(DESCRIPTOR);
com.example.aidl.IRemoteCallback _arg0;
_arg0 = com.example.aidl.IRemoteCallback.Stub.asInterface(data.readStrongBinder());
this.unregister(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_execute:
{
data.enforceInterface(DESCRIPTOR);
this.execute();
reply.writeNoException();
return true;
}
case TRANSACTION_getStatus:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
int _result = this.getStatus(_arg0);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.example.aidl.IRemoteService
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInterfaceDescriptor()
{
return DESCRIPTOR;
}
//注冊回調(diào)
public void register(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongBinder((((callback!=null))?(callback.asBinder()):(null)));
mRemote.transact(Stub.TRANSACTION_register, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
//取消注冊回調(diào)
public void unregister(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeStrongBinder((((callback!=null))?(callback.asBinder()):(null)));
mRemote.transact(Stub.TRANSACTION_unregister, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
//執(zhí)行回調(diào)
public void execute() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_execute, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
//獲取狀態(tài)
public int getStatus(java.lang.String flag) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(flag);
mRemote.transact(Stub.TRANSACTION_getStatus, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_register = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_unregister = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_execute = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_getStatus = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
}
//注冊回調(diào)
public void register(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException;
//取消注冊回調(diào)
public void unregister(com.example.aidl.IRemoteCallback callback) throws android.os.RemoteException;
//執(zhí)行回調(diào)
public void execute() throws android.os.RemoteException;
//獲取狀態(tài)
public int getStatus(java.lang.String flag) throws android.os.RemoteException;
}
在ClientActivity綁定遠(yuǎn)程Service并建立連接時會調(diào)用ServiceConnection.onServiceConnected(ComponentName name, IBinder service)
[java] view plaincopy
public void onServiceConnected(ComponentName name, IBinder service) {
remoteService = IRemoteService.Stub.asInterface(service);
//注冊回調(diào)
try {
remoteService.register(remoteCallback);
} catch (RemoteException e) {
e.printStackTrace();
}
}
IBinder service是從RemoteService返回的IRemoteService.Stub iBinder,這個對象是Server應(yīng)用進(jìn)程中的對象。
IRemoteService.Stub.asInterface(service)在本地創(chuàng)建了一個代理
public static com.example.aidl.IRemoteService asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = (android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);//這里肯定返回null
if (((iin!=null)&&(iin instanceof com.example.aidl.IRemoteService))) {
return ((com.example.aidl.IRemoteService)iin);
}
return new com.example.aidl.IRemoteService.Stub.Proxy(obj);//創(chuàng)建一個本地代理
}
當(dāng)使用remoteService調(diào)用方法時,其實是調(diào)用了本地com.example.aidl.IRemoteService.Stub.Proxy對象的方法,從Proxy方法中可以看到,每個方法都執(zhí)行了mRemote.transact(Stub.TRANSACTION_xxx, _data, _reply, 0);。
如:
[java] view plaincopy
//獲取狀態(tài)
public int getStatus(java.lang.String flag) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeString(flag);
mRemote.transact(Stub.TRANSACTION_getStatus, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
這一過程是把Client端的參數(shù)轉(zhuǎn)換成Parcel(_data)傳遞到Server端,而在Server端又會把返回數(shù)據(jù)保存到_reply中,這就形成了一次交互。
mRemote是遠(yuǎn)程對象,transact方法會執(zhí)行onTransact方法
[java] view plaincopy
public final boolean transact(int code, Parcel data, Parcel reply,
int flags) throws RemoteException {
if (Config.LOGV) Log.v("Binder", "Transact: " + code + " to " + this);
if (data != null) {
data.setDataPosition(0);
}
boolean r = onTransact(code, data, reply, flags);
if (reply != null) {
reply.setDataPosition(0);
}
return r;
}
這樣就會執(zhí)行遠(yuǎn)程的onTransact方法,
[java] view plaincopy
case TRANSACTION_getStatus:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
int _result = this.getStatus(_arg0);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
注意 int _result = this.getStatus(_arg0);,這就調(diào)用了Server端的getStatus(String flag),并把返回結(jié)果寫到Client端的代理Proxy對象的_reply中
到此,aidl通信過程就完成了。
PS: aidl通信有點復(fù)雜,但仔細(xì)分析并不是很難
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。