系统提供了AIDL方式来实现Bindler机制。
1.创建AIDL接口
创建一个aidl类型文件,这里我们创建IBookManager.aidl
interface IBookManager {
void addBook(in Book book);
List<Book> getBookList();
}
Buid后,系统在build/generated/下创建BookManager.java文件,也就是BookManager类。
/*
* This file is auto-generated. DO NOT MODIFY.
* Original file: /Users/wangliang/workspace/Study/app/src/main/aidl/com/moreunion/study/book/IBookManager.aidl
*/
package com.moreunion.study.book;
// 定义的一个接口,里面有两个方法:getBookList和addtBook
// getBookList用于从远程服务端获取图书列表
// addBook用于向远程服务端添加一本图书
public interface IBookManager extends android.os.IInterface {
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.moreunion.study.book.IBookManager {
private static final java.lang.String DESCRIPTOR = "com.moreunion.study.book.IBookManager";
/** Construct the stub at attach it to the interface. */
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.moreunion.study.book.IBookManager interface,
* generating a proxy if needed.
*/
public static com.moreunion.study.book.IBookManager asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.moreunion.study.book.IBookManager))) {
return ((com.moreunion.study.book.IBookManager)iin);
}
return new com.moreunion.study.book.IBookManager.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 {
java.lang.String descriptor = DESCRIPTOR;
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(descriptor);
return true;
}
case TRANSACTION_getBookList: {
data.enforceInterface(descriptor);
java.util.List<com.moreunion.study.Book> _result = this.getBookList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_addBook: {
data.enforceInterface(descriptor);
com.moreunion.study.Book _arg0;
if ((0!=data.readInt())) {
_arg0 = com.moreunion.study.Book.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.addBook(_arg0);
reply.writeNoException();
return true;
}
default: {
return super.onTransact(code, data, reply, flags);
}
}
}
private static class Proxy implements com.moreunion.study.book.IBookManager {
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;
}
@Override
public java.util.List<com.moreunion.study.Book> getBookList() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<com.moreunion.study.Book> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.moreunion.study.Book.CREATOR);
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public void addBook(com.moreunion.study.Book book) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((book!=null)) {
_data.writeInt(1);
book.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_getBookList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_addBook = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
public java.util.List<com.moreunion.study.Book> getBookList() throws android.os.RemoteException;
public void addBook(com.moreunion.study.Book book) throws android.os.RemoteException;
}
客户端跟服务端都需要生成IBookManager类
内部静态类Study(就是一个Bindler类)以及Study的内部静态类Proxy(一个IBookManger类)。
Study类
/** Local-side IPC implementation stub class. */
public static abstract class Stub extends android.os.Binder implements com.moreunion.study.book.IBookManager
{
private static final java.lang.String DESCRIPTOR = "com.moreunion.study.book.IBookManager";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an com.moreunion.study.book.IBookManager interface,
* generating a proxy if needed.
*/
public static com.moreunion.study.book.IBookManager asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof com.moreunion.study.book.IBookManager))) {
return ((com.moreunion.study.book.IBookManager)iin);
}
return new com.moreunion.study.book.IBookManager.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
{
java.lang.String descriptor = DESCRIPTOR;
switch (code)
{
case INTERFACE_TRANSACTION:
{
reply.writeString(descriptor);
return true;
}
case TRANSACTION_getBookList:
{
data.enforceInterface(descriptor);
java.util.List<com.moreunion.study.Book> _result = this.getBookList();
reply.writeNoException();
reply.writeTypedList(_result);
return true;
}
case TRANSACTION_addBook:
{
data.enforceInterface(descriptor);
com.moreunion.study.Book _arg0;
if ((0!=data.readInt())) {
_arg0 = com.moreunion.study.Book.CREATOR.createFromParcel(data);
}
else {
_arg0 = null;
}
this.addBook(_arg0);
reply.writeNoException();
return true;
}
default:
{
return super.onTransact(code, data, reply, flags);
}
}
Proxy类
private static class Proxy implements com.moreunion.study.book.IBookManager {
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;
}
@Override
public java.util.List<com.moreunion.study.Book> getBookList() throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
java.util.List<com.moreunion.study.Book> _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getBookList, _data, _reply, 0);
_reply.readException();
_result = _reply.createTypedArrayList(com.moreunion.study.Book.CREATOR);
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public void addBook(com.moreunion.study.Book book) throws android.os.RemoteException {
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
try {
_data.writeInterfaceToken(DESCRIPTOR);
if ((book!=null)) {
_data.writeInt(1);
book.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_addBook, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
}
IBookManager类
public interface IBookManager extends android.os.IInterface {
public java.util.List<com.moreunion.study.Book> getBookList() throws android.os.RemoteException;
public void addBook(com.moreunion.study.Book book) throws android.os.RemoteException;
}
服务端需要实现Study抽象类,在onBinder()方法中返回Study类的对象。
客户端绑定服务,成功后,服务端返回一个Binder对象(继承Study类的对象)给客户端,客户端把Bindler对象当作参数调用Study类中的静态方法asInterface()返回一个IBookManager对象。
就这样我们使用IBindler机制完成了进程间通信。