public interface ICat extends android.os.IInterface
{
public java.lang.String getColor() throws android.os.RemoteException;
public double getWeight() throws android.os.RemoteException;
/* 对于服务端不需要上述接口,调用通过消息解调得到 */
/** Local-side IPC implementation stub class. */
====================================================================== stub
Stub提供了服务端的数据包解析,他需要本地的服务提供实际的数据。用户必须重载提供数据。
public static abstract class Stub extends android.os.Binder implements org.crazyit.service.ICat
{
private static final java.lang.String DESCRIPTOR = "org.crazyit.service.ICat";
/** Construct the stub at attach it to the interface. */
public Stub()
{
this.attachInterface(this, DESCRIPTOR);
}
/**
* Cast an IBinder object into an org.crazyit.service.ICat interface,
* generating a proxy if needed.
*/
public static org.crazyit.service.ICat asInterface(android.os.IBinder obj)
{
if ((obj==null)) {
return null;
}
android.os.IInterface iin = obj.queryLocalInterface(DESCRIPTOR);
if (((iin!=null)&&(iin instanceof org.crazyit.service.ICat))) {
return ((org.crazyit.service.ICat)iin);
}
return new org.crazyit.service.ICat.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_getColor:
{
data.enforceInterface(DESCRIPTOR);
java.lang.String _result = this.getColor();
reply.writeNoException();
reply.writeString(_result);
return true;
}
case TRANSACTION_getWeight:
{
data.enforceInterface(DESCRIPTOR);
double _result = this.getWeight();
reply.writeNoException();
reply.writeDouble(_result);
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
×××××××××××××××××××××××××××××××××××××××××××××××××××××××× Proxy
运行在client空间,在client使用asInterface函数和远端IBinder构造
private static class Proxy implements org.crazyit.service.ICat
{
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.lang.String getColor() 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);
mRemote.transact(Stub.TRANSACTION_getColor, _data, _reply, 0);
_reply.readException();
_result = _reply.readString();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
@Override public double getWeight() throws android.os.RemoteException
{
android.os.Parcel _data = android.os.Parcel.obtain();
android.os.Parcel _reply = android.os.Parcel.obtain();
double _result;
try {
_data.writeInterfaceToken(DESCRIPTOR);
mRemote.transact(Stub.TRANSACTION_getWeight, _data, _reply, 0);
_reply.readException();
_result = _reply.readDouble();
}
finally {
_reply.recycle();
_data.recycle();
}
return _result;
}
}
×××××××××××××××××××××××××××××××××××××××××××××××× Proxy
static final int TRANSACTION_getColor = (android.os.IBinder.FIRST_CALL_TRANSACTION + 0);
static final int TRANSACTION_getWeight = (android.os.IBinder.FIRST_CALL_TRANSACTION + 1);
}
}
public class Binder implements IBinder {
private IInterface mOwner; /× 存储了实际的接口对象,可以为自己 ×/
private String mDescriptor;
}
public interface IInterface
{
public IBinder asBinder();
}
关于Messenger的接口。
IMessenge.Proxy -> IMessenger -> IInterface (client side interface object)
IMessenge.Stub -> IMessenger -> IInterface (server side interface object)
IMessenge.Stub -> Binder -> IBinder
IMessenge.Proxy wrap pointer to IMessenge.Stub,the pointer maybe Binder
BinderProxy -> IBinder 可能是客户在本端的IBinder对象。
Handler <<– >> MessengerImpl -> IMessenger.Stub
Messenger ->> MessengerImpl –>> Handler