1 handler的使用
每个线程可以有多个handle,但只能有1个looper,也只有1个MessageQuene
Handler:添加消息到消息队列、处理Looper分发过来的消息
Message:存储通信信息 在handler发出消息后 message会与对应handler绑定
private boolean enqueueMessage(@NonNull MessageQueue queue, @NonNull Message msg,
long uptimeMillis) {
msg.target = this;
msg.workSourceUid = ThreadLocalWorkSource.getUid();
if (mAsynchronous) {
msg.setAsynchronous(true);
}
return queue.enqueueMessage(msg, uptimeMillis);
}
MessageQuene:存储和管理Handler发送来的信息
单向链表存储结构 在Looper创建时同步创建
private Looper(boolean quitAllowed) {
mQueue = new MessageQueue(quitAllowed);
mThread = Thread.currentThread();
}
Looper:调度器,从消息队列取出消息并分发给Handler
从消息队列中取出消息分发给该message所属的target,然后由这个handler去处理该message。