【移动应用开发技术】深入分析AIDL原理_第1页
【移动应用开发技术】深入分析AIDL原理_第2页
【移动应用开发技术】深入分析AIDL原理_第3页
【移动应用开发技术】深入分析AIDL原理_第4页
【移动应用开发技术】深入分析AIDL原理_第5页
已阅读5页,还剩6页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】深入分析AIDL原理

在上一篇文章(Service使用方式)中,介绍了Android进程间通信(IPC)的使用,并给出了一个示例。但并没有深入分析aidl是怎样可以做到进程间通信的,它的执行过程是怎样的?这篇文章来分析IRemoteService.aidl的执行过程,并理解aidl是怎样跨进程通信的。

当我们创建IRemoteService.aidl文件时,IDE会为我们在gen目录中创建相应的文件。[java]

viewplaincopy/**

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;

}

//注册回调

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();

}

}

//取消注册回调

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();

}

}

//执行回调

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();

}

}

//获取状态

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);

}

//注册回调

public

void

register(com.example.aidl.IRemoteCallback

callback)

throws

android.os.RemoteException;

//取消注册回调

public

void

unregister(com.example.aidl.IRemoteCallback

callback)

throws

android.os.RemoteException;

//执行回调

public

void

execute()

throws

android.os.RemoteException;

//获取状态

public

int

getStatus(java.lang.String

flag)

throws

android.os.RemoteException;

}

在ClientActivity绑定远程Service并建立连接时会调用ServiceConnection.onServiceConnected(ComponentNamename,IBinderservice)[java]

viewplaincopypublic

void

onServiceConnected(ComponentName

name,

IBinder

service)

{

remoteService

=

IRemoteService.Stub.asInterface(service);

//注册回调

try

{

remoteService.register(remoteCallback);

}

catch

(RemoteException

e)

{

e.printStackTrace();

}

}

IBinderservice是从RemoteService返回的IRemoteService.StubiBinder,这个对象是Server应用进程中的对象。IRemoteService.Stub.asInterface(service)在本地创建了一个代理

publicstaticcom.example.aidl.IRemoteServiceasInterface(android.os.IBinderobj){if((obj==null)){returnnull;}android.os.IInterfaceiin=(android.os.IInterface)obj.queryLocalInterface(DESCRIPTOR);//这里肯定返回nullif(((iin!=null)&&(iininstanceofcom.example.aidl.IRemoteService))){return((com.example.aidl.IRemoteService)iin);}returnnewcom.example.aidl.IRemoteService.Stub.Proxy(obj);//创建一个本地代理}

当使用remoteService调用方法时,其实是调用了本地com.example.aidl.IRemoteService.Stub.Proxy对象的方法,从Proxy方法中可以看到,每个方法都执行了mRemote.transact(Stub.TRANSACTION_xxx,_data,_reply,0);。如:[java]

viewplaincopy//获取状态

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端的参数转换成Parcel(_data)传递到Server端,而在Server端又会把返回数据保存到_reply中,这就形成了一次交互。mRemote是远程对象,transact方法会执行onTransact方法[java]

viewplaincopypublic

final

boolean

transact(int

code,

Par

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论