一、 服务端
连接成功服务端会通过public IBinder onBind(Intent intent)方法
调用 return new IBookManager.Stub()将stub(也就是binder)类返回给客户端
由于此类实现了IBookManager接口所以要实现已下五个方法(此方法就是在aidl接口文件定义的方法)
并已下五个方法中做业务逻辑
1、int add(int i,int j);
2、String getUserInfo(in Book book);
3、void getOutList(out String[] list);
4、void setInList(in String[] list);
5、void getInOutList(inout String[] list);
二、客户端
连接成功后 客户端会回调
public void onServiceConnected(ComponentName componentName, IBinder iBinder)方法
并在此方法中调用iBase = IBookManager.Stub.asInterface(iBinder)方法
得到 IBookManager 类 也就是proxy类
Proxy类实现了IBookManager接口 也要实现了已下五个方法,此五种方法是给客户端调用的
1、int add(int i,int j);
2、String getUserInfo(in Book book);
3、void getOutList(out String[] list);
4、void setInList(in String[] list);
5、void getInOutList(inout String[] list);
当客户端调用 iBase.add(10,20),实际调用的是 proxy中的add()方法如下:
public int add(int i, int j) throws android.os.RemoteException {
mRemote其实就是 stub(也就是binder)
mRemote调用transact(Stub.TRANSACTION_add, _data, _reply, 0)方法时
就不会走下面的方法了,此时在stub(也就是binder)类中的onTransact() 就会回调,
在onTransact()中根据方法标示 取出_data和_reply 并调用 this.add(_arg0, _arg1);
也就是调用在服务端实现了的 add()方法 把返回结果放在_reply中 返回true
onTransact()返回true后 就继续走下面的方法 最后把结果返回给客户端
android.os.Parcel _data = android.os.Parcel.obtain();//把方法需要的参数写入这里面(如果方法有参数)
android.os.Parcel _reply = android.os.Parcel.obtain();//把方法返回值写入这里面(如果方法有返回值)
int _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
_data.writeInt(i);
_data.writeInt(j);
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
public class sdsds {
public interface IBookManager extends android.os.IInterface {
public static abstract class Stub extends android.os.Binder implements screen.com.mybinder.IBookManager {
private static final java.lang.String DESCRIPTOR = "screen.com.mybinder.IBookManager";
public Stub() {
this.attachInterface(this, DESCRIPTOR);
}
public static screen.com.mybinder.IBookManager asInterface(android.os.IBinder obj) {
if ((obj == null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
//如果在一个进程
if (((iin != null) && (iin instanceof screen.com.mybinder.IBookManager))) {
return ((screen.com.mybinder.IBookManager) iin);
}
//客户端服务端不再一个线程 大多数走这个方法
return new screen.com.mybinder.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 {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_add: {
data.enforceInterface(DESCRIPTOR);
int _arg0;
_arg0 = data.readInt();
int _arg1;
_arg1 = data.readInt();
int _result = this.add(_arg0, _arg1);
reply.writeNoException();
reply.writeInt(_result);
return true;
}
case TRANSACTION_getUserInfo: {
data.enforceInterface(DESCRIPTOR);
screen.com.mybinder.Book _arg0;
if ((0 != data.readInt())) {
_arg0 = screen.com.mybinder.Book.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
java.lang.String _result = this.getUserInfo(_arg0);
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_getOutList: {
data.enforceInterface(DESCRIPTOR);
java.lang.String[] _arg0;
int _arg0_length = data.readInt();
if ((_arg0_length < 0)) {
_arg0 = null;
} else {
_arg0 = new java.lang.String[_arg0_length];
}
this.getOutList(_arg0);
reply.writeNoException();
reply.writeStringArray(_arg0);
return true;
}
case TRANSACTION_setInList: {
data.enforceInterface(DESCRIPTOR);
java.lang.String[] _arg0;
_arg0 = data.createStringArray();
this.setInList(_arg0);
reply.writeNoException();
return true;
}
case TRANSACTION_getInOutList: {
data.enforceInterface(DESCRIPTOR);
java.lang.String[] _arg0;
_arg0 = data.createStringArray();
this.getInOutList(_arg0);
reply.writeNoException();
reply.writeStringArray(_arg0);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
private static class Proxy implements screen.com.mybinder.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 int add(int i, int j) 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.writeInt(i);
_data.writeInt(j);
mRemote.transact(Stub.TRANSACTION_add, _data, _reply, 0);
_reply.readException();
_result = _reply.readInt();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public java.lang.String getUserInfo(screen.com.mybinder.Book book) 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.writeInterfaceToken(DESCRIPTOR);
if ((book != null)) {
_data.writeInt(1);
book.writeToParcel(_data, 0);
} else {
_data.writeInt(0);
}
mRemote.transact(Stub.TRANSACTION_getUserInfo, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
} finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override
public void getOutList(java.lang.String[] list) 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 ((list == null)) {
_data.writeInt(-1);
} else {
_data.writeInt(list.length);
}
mRemote.transact(Stub.TRANSACTION_getOutList, _data, _reply, 0);
_reply.readException();
_reply.readStringArray(list);
} finally {
_reply.recycle();
_data.recycle();
}
}
@Override
public void setInList(java.lang.String[] list) 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.writeStringArray(list);
mRemote.transact(Stub.TRANSACTION_setInList, _data, _reply, 0);
_reply.readException();
} finally {
_reply.recycle();
_data.recycle();
}
}
@Override
public void getInOutList(java.lang.String[] list) 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.writeStringArray(list);
mRemote.transact(Stub.TRANSACTION_getInOutList, _data, _reply, 0);
_reply.readException();
_reply.readStringArray(list);
} finally {
_reply.recycle();
_data.recycle();
}
}
}
static final int TRANSACTION_add = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getUserInfo = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
static final int TRANSACTION_getOutList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 2);
static final int TRANSACTION_setInList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 3);
static final int TRANSACTION_getInOutList = (android.os.IBinder.FIRST_CALL_TRANSACTION + 4);
}
public int add(int i, int j) throws android.os.RemoteException;
public java.lang.String getUserInfo(screen.com.mybinder.Book book) throws android.os.RemoteException;
public void getOutList(java.lang.String[] list) throws android.os.RemoteException;
public void setInList(java.lang.String[] list) throws android.os.RemoteException;
public void getInOutList(java.lang.String[] list) throws android.os.RemoteException;
}
}