<pre name="code" class="java">一.使用:参考file:///E:/sdk/adtbundlewindows64/sdk/docs/guide/components/bound-services.html#Binding
Using Messager
二.源码分析
IMessage.aidl
/** @hide */
oneway interface IMessenger {
void send(in Message msg);
}
1.IMessage.java源码分析
// 一.定义一个新接口:IMessage接口--相当于IPlayer(参考:IPC的Proxy-Stub设计模式c: 4.谁来写Proxy及Stub类呢?)
public interface IMessenger extends IInterface {
// 二.撰写一个Stub类:Stub--相当于PlayerStub
public static abstract class Stub extends Binder implements
IMessenger { // 继承IMessage接口,该接口函数:send
...
/**
* Cast an IBinder object into an IMessenger interface,
* generating a proxy if needed.
*/
public static IMessenger asInterface(IBinder obj) {
...
return new IMessenger.Stub.Proxy(obj);
}
public IBinder asBinder() {
return this;
}
// 服务器端核心方法
@Override
public boolean onTransact(int code, Parcel data,
Parcel reply, int flags)
throws RemoteException {
switch (code) {
case INTERFACE_TRANSACTION: {
reply.writeString(DESCRIPTOR);
return true;
}
case TRANSACTION_send: { // 接收的code和发送的code都是TRANSACTION_send
Message _arg0;
if ((0 != data.readInt())) { // data.readInt为1(当msg不为空时)
_arg0 = Message.CREATOR.createFromParcel(data);
} else {
_arg0 = null;
}
this.send(_arg0); // 看谁实现了IMessage.Stub的send方法,具体就调用那个send方法
// 实质实现:MessengerImpl类,该类是Handler的内部类
return true;
}
}
return super.onTransact(code, data, reply, flags);
}
// 三.撰写一个Proxy类---类似PlayerProxy
private static class Proxy implements IMessenger {
private IBinder mRemote;
Proxy(IBinder remote) {
mRemote = remote;
}
public IBinder asBinder() {
return mRemote;
}
// 客户端核心方法:客户端调用的唯一方法
public void send(Message msg)
throws RemoteException {
Parcel _data = Parcel.obtain();
// 包装输入数据(Message)
if ((msg != null)) { // msg不为空
_data.writeInt(1);
msg.writeToParcel(_data, 0);// 将msg所有字段(what,obj)写到_data中去
} else { // msg为空
_data.writeInt(0);
}
// 核心代码:客户端发送数据
mRemote.transact(Stub.TRANSACTION_send, _data, null, // 接收的code和发送的code都是TRANSACTION_send
IBinder.FLAG_ONEWAY);
}
}
static final int TRANSACTION_send = (IBinder.FIRST_CALL_TRANSACTION + 0);
}
// 接口函数
public void send(Message msg) throws RemoteException;
}
1.入口1:final Messenger mMessenger = new Messenger(new IncomingHandler());
public Messenger(Handler target) {
mTarget = target.getIMessenger();
}
final IMessenger getIMessenger() {
mMessenger = new MessengerImpl();
return mMessenger;
}
服务器端实现Binder的Stub的接口方法(send)
Handler.java类中:
private final class MessengerImpl extends IMessenger.Stub {
public void send(Message msg) {
Handler.this.sendMessage(msg); // 最终:调用handler发送消息
}
}
三.总结
1.工作机制: // 本质:封装Binder的transact和onTransact
客户端:transact方法-->具体包装:IMessage.Proxy.send方法
服务器端:onTransact方法-->具体包装:IMessage.Stub.onTransact
Handler.MessengerImpl实现了Stub的接口方法send,并且onTransact调用send方法,就是调用MessengerImpl的send方法,
最终调用Handler的sendMessage方法
2.流程:
客户端:调用IMessage.Proxy.send发送一个Message对象(本质调用Binder的transact发送Message)
服务器端:IMessage.Stub.onTransact收到,onTransact被调用,解析出客户端发送过来的Message对象,把Message发送给Handler,让
Handler去处理(handleMessage)
Messenger源码分析
最新推荐文章于 2022-02-02 00:27:20 发布