- 创建两个业务模块IBookManager及IStudentManager
业务一:查询图书
业务二:查询学生
查询图书aidl
服务端查询图书类BookManager
public class BookManager extends IBookManager.Stub {
public BookManager() {
}
@Override
public List<Book> getBookList() throws RemoteException {
return null;
}
@Override
public void addBook(Book book) throws RemoteException {
}
@Override
public void registerListener(IOnNeedBookArrivedListener listener) throws RemoteException {
}
@Override
public void unregisterListener(IOnNeedBookArrivedListener listener) throws RemoteException {
}
}
查询学生aidl
服务端查询学生类StudentManager。
public class StudentManager extends IStudentManager.Stub {
public StudentManager() {
}
@Override
public List<Student> getStudent() throws RemoteException {
return null;
}
@Override
public void addStudent(Student student) throws RemoteException {
}
@Override
public void registerListener(IOnNeedStudentArriedListener listener) throws RemoteException {
}
@Override
public void unregisterListener(IOnNeedStudentArriedListener listener) throws RemoteException {
}
}
- 创建Binder连接池接口IBinderPool
//系统默认生成的代码
interface IBinderPool {
/**
* Demonstrates some basic types that you can use as parameters
* and return values in AIDL.
*/
void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
double aDouble, String aString);
}
修改IBinderPool.aidl接口
interface IBinderPool {
//只有一个方法来,根据参数来获取服务端的业务Binder对象
IBinder queryBindler(int bindlerType);
}
Build之后生成IBinderPool.java接口,主要查看一下代理类Proxy及Bindler实现类Study。
客户端绑定Service,Service端返回IBinderPool类的对象(Binder对象)。客户端调用Study的静态方法asInterface(Binder binder)获取IBinderPool类的对象(对Binder对象进行了封装)。
adInsterface()运行在客户端。
public static com.test.pool.BindlerPool asInterface(android.os.IBinder obj) {
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.test.pool.BindlerPool))) {
return ((com.test.pool.BindlerPool)iin);
}
return new com.test.pool.BindlerPool.Stub.Proxy(obj);
}
判断Binder对象处于哪个进程中的方法queryLocalInterface()。
queryLocalInterface()运行在客户端。
public @Nullable IInterface queryLocalInterface(@NonNull String descriptor) {
if (mDescriptor != null && mDescriptor.equals(descriptor)) {
return mOwner;
}
return null;
}
这里我们说明的是不同进程间通信,所以这里就直接new了代理类的对象,Binder对象作为参数传递给Proxy的构造函数。
return new com.test.pool.BindlerPool.Stub.Proxy(obj);
代理类Proxy的源码如下,客户端通过Proxy对象的queryBindler()来获取业务逻辑相关的Binder对象。
private static class Proxy implements com.test.pool.BindlerPool {
private android.os.IBinder mRemote;
Proxy(android.os.IBinder remote) {
mRemote = remote;
}
@Override
public android.os.IBinder asBinder() {
return mRemote;
}
public java.lang.String getInterfaceDescriptor() {
return DESCRIPTOR;
}
//queryBindler()方法运行在客户端,调用线程挂起状态
@Override
public android.os.IBinder queryBindler(int bindlerType) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
android.os.IBinder _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(bindlerType);
//进程间传输方法,这里让线程挂起的具体方法实现
mRemote.transact(Stub.TRANSACTION_queryBindler, _data, _reply, 0);
_reply.readException();
//在客户端执行,从内存中获取查询到的Binder对象
_result = _reply.readStrongBinder();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
transact()方法实现,Binder类中的方法。
public final boolean transact(int code, @NonNull Parcel data, @Nullable Parcel reply,
int flags) throws RemoteException {
if (false) 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;
}
onTransact()方法运行在Service端所在进程,把Service端创建的Binder对象写入到内存中reply.writeStrongBinder(_result);
@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) {
case INTERFACE_TRANSACTION: {
reply.writeString(descriptor);
return true;
}
case TRANSACTION_queryBindler: {
data.enforceInterface(descriptor);
int _arg0;
_arg0 = data.readInt();
//根据查询类型创建Binder对象
android.os.IBinder _result = this.queryBindler(_arg0);
reply.writeNoException();
//把Binder对象写入到内存中
reply.writeStrongBinder(_result);
return true;
}
default: {
return super.onTransact(code, data, reply, flags);
}
}
客户端从内存中获取Binder对象。
//queryBindler()方法运行在客户端,调用线程挂起状态
@Override
public android.os.IBinder queryBindler(int bindlerType) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
android.os.IBinder _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(bindlerType);
//进程间传输方法,这里让线程挂起的具体方法实现
mRemote.transact(Stub.TRANSACTION_queryBindler, _data, _reply, 0);
_reply.readException();
//在客户端执行,从内存中获取查询到的Binder对象
_result = _reply.readStrongBinder();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
客户端获取到业务Binder对象后,调用Study的静态方法asInterface(Binder binder),来创建代理类Proxy的对象。
- 创建Binder连接池具体实现类BinderPool
BinderPool类中实现了绑定服务并获取
public class BinderPool {
public static final int BINDER_BOOK = 0;
public static final int BINDER_STUDENT = 1;
private static BinderPool mBinderPool;
private BinderPool(Context context) {
contentService(context);
}
public static BinderPool getInstance(Context context) {
if (mBinderPool == null) {
synchronized (BinderPool.class) {
mBinderPool = new BinderPool(context);
}
}
return mBinderPool;
}
private void contentService(Context context) {
Intent intent = new Intent(context, BinderPoolService.class);
context.bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
}
@Override
public void onServiceDisconnected(ComponentName name) {
}
};
public static class BinderPoolImp extends IBinderPool.Stub {
public BinderPoolImp() {
}
/**
* 客户端获取到的IBinder对象与服务端创建的是同一个对象
* @param binderType
* @return
* @throws RemoteException
*/
@Override
public IBinder queryBinder(int binderType) throws RemoteException {
IBinder mBinder = null;
switch (binderType) {
case BINDER_STUDENT:{
mBinder = new StudentManager();
}
break;
case BINDER_BOOK:{
mBinder = new BookManager();
}
break;
default:{
}
break;
}
return mBinder;
}
}
}
- 创建客户端测试
public void doWork() {
BinderPool binderPool = BinderPool.getInstance(BindPoolActivity.this);
//查询图书
IBinder bookBinder = binderPool.queryBinder(BinderPool.BIND_BOOK);
IBookManager bookManager = (IBookManager)BookManager.asInterface(bookBinder);
try{
bookManager.addBook(new Book(1, "《Android开发艺术探索》"));
List<Book> listBooks = bookManager.getBookList();
} catch(RemoteException e) {
}
//查询学生
IBinder studentBinder = binderPool.query(BinderPool.BIND_STUDENT);
IStudentManager studentManager = StudentManager.asInterface(studentBinder);
try {
studentManger.addStudent(18, "孙正义");
List<Student> lsitStudents = studentManager.getStudentList();
} catch(RemoteException e) {
}
}