Android进程间通信 — AIDL
一、前言
Android中的RPC参考了JAVA中的RMI(remote methord invocation)方案;Android中的RPC机制是为了实现进程间通信,即一个进程使用另一个进程中的远程对象,Android用AIDL(android inte易做图ce defined language,Android接口定义语言)来实现,使用户可以很方便的定义一套接口,然后通过远程Service为代理,客户端在绑定该远程Service时,获得远程对象,然后就可以使用该对象。
二、流程
1. 定义一个.aidl接口文件,ADT则会在gen目录下,自动生成一个对应的.java文件(此文件为自动生成,不可修改);
2. 自动生成的.java中,有一个抽象的Stub类,因此,创建一个基于Stub的子类,并实现在.aidl中定义的接口;
3. 创建一个Service子类,在onCreate中,创建步骤2中的实例对象,在onBind中,返回该实例对象;
4. 在Activity中,发送一个bindService(it, conn, BIND_AUTO_CREATE),请求绑定远程Service;
5. 若成功,则在ServiceConnection.onServiceConnected中获得步骤3中的实例对象;
6. 在AndroidManifest.xml中,注册远程Service;
三、例子
按照流程中的步骤,实现该例子:
3.1 定义.aidl文件
[java]
package com.chris.rpc.aidl;
inte易做图ce ITest {
void setCustomerName(String name);
String getCustomerName();
}
package com.chris.rpc.aidl;
inte易做图ce ITest {
void setCustomerName(String name);
String getCustomerName();
}很简单,定义了两个接口,然后,ADT就会自动在gen目录下生成ITest.java文件,我们来看看:
[java]
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: E:\\chris\\Android_Dev\\eclipse\\workspace\\RpcAidl\\src\\com\\chris\\rpc\\aidl\\ITest.aidl
*/
package com.chris.rpc.aidl;
public inte易做图ce ITest extends android.os.IInte易做图ce
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.chris.rpc.aidl.ITest
{
private static final java.lang.String DESCRIPTOR = "com.chris.rpc.aidl.ITest";
/** Construct the stub at attach it to the inte易做图ce. */
public Stub()
{
this.attachInte易做图ce(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.chris.rpc.aidl.ITest inte易做图ce,
* generating a proxy if needed.
*/
public static com.chris.rpc.aidl.ITest asInte易做图ce(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInte易做图ce iin = obj.queryLocalInte易做图ce(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.chris.rpc.aidl.ITest))) {
return ((com.chris.rpc.aidl.ITest)iin);
}
return new com.chris.rpc.aidl.ITest.Stub.Proxy(obj);
}
@Override 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_setCustomerName:
{
data.enforceInte易做图ce(DESCRIPTOR);
java.lang.String _arg0;
_arg0 = data.readString();
this.setCustomerName(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_getCustomerName:
{
data.enforceInte易做图ce(DESCRIPTOR);
java.lang.String _result = this.getCustomerName();
reply.writeNoException();
reply.writeString(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements com.chris.rpc.aidl.ITest
{
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote)
{
mRemote = remote;
}
@Override public android.os.IBinder asBinder()
{
return mRemote;
}
public java.lang.String getInte易做图ceDescriptor()
{
return DESCRIPTOR;
}
@Override public void setCustomerName(java.lang.String name) throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInte易做图ceToken(DESCRIPTOR);
_data.writeString(name);
mRemote.transact(Stub.TRANSACTION_setCustomerName, _data, _reply, 0);
_reply.readException();
}
finally {
_reply.recycle();
_data.recycle();
}
}
@Override public java.lang.String getCustomerName() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.lang.String _result;
try {
_data.writeInte易做图ceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getCustomerName, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
static final int TRANSACTION_setCustomerName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getCustomerName = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public void setCustomerName(java.lang.String name) throws android.os.RemoteException;
public java.lang.String getCustomerName() throws android.os.RemoteException;
}
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: E:\\chris\\Android_Dev\\eclipse\\workspace\\RpcAidl\\src\\com\\chris\\rpc\\aidl\\ITest.aidl
*/
package com.chris.rpc.aidl;
public inte易做图ce ITest extends android.os.IInte易做图ce
{
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.chris.rpc.aidl.ITest
{
private static final java.lang.String DESCRIPTOR = "com.chris.rpc.aidl.ITest";
/** Construct the stub at attach it to the inte易做图ce. */
public Stub()
{
this.attachInte易做图ce(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.chris.rpc.aidl.ITest inte易做图ce,
* generating a proxy if needed.
*/
public static com.chris.rpc.aidl.ITest asInte易做图ce(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInte易做图ce iin = obj.queryLocalInte易做图ce(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.chris.rpc.aidl.ITest))) {
return ((com.chris.rpc.aidl.ITest)iin);
}
return new com.chris.rpc.aidl.ITest.Stub.Proxy(obj);
}
@Override 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_setCustomerName:
{
data.enforceInte易做图ce(DESCRIPTOR);
java.lang.String _ar
补充:移动开发 , Android ,