Handler机制中的消息队列

学习自蘑菇街大佬

Handler机制可以看成是一个消息阻塞队列,当有消息时立即处理消息,没有消息时则阻塞.在Android系统中APP启动后很快进入死循环,不断读取MessageQueue中的消息,有消息则立即处理,没有消息则阻塞.Android的View绘制,事件响应(点击,触摸屏幕等)都是把消息发送到了主线程的消息队列,包括自己在主线程创建的handler最终也是把消息插入到了主线程消息队列中,并最终分发到到指定的handler处理消息.

handler.send(msg)
->sendMessageDelayed(msg, 0)
->sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis)
->enqueueMessage(queue, msg, uptimeMillis)
//这里就是进入到消息队列,进入的是主线程的Looper(MainLooper)
->queue.enqueueMessage(msg, uptimeMillis);

当出队的时候会根据msg中的一个成员变量target(这个target就是handler)来分发的对应的handler,这样handler就拿到了信息,

for (;;) {
       // 不断从 MessageQueue 获取 消息
        Message msg = queue.next(); // might block
        //退出 Looper 
        if (msg == null) {
            // No message indicates that the message queue is quitting.
           return;
        }
        //...
        try {
            msg.target.dispatchMessage(msg);
            end = (slowDispatchThresholdMs == 0) ? 0 : SystemClock.uptimeMillis();
        } finally {
            //...
        }

这个target是怎么来的呢,Message有几个静态方法可以传入Handler实例作为target的值

public static Message obtain(Handler h);
public static Message obtain(Handler h, Runnable callback) ;
public static Message obtain(Handler h, int what);
public static Message obtain(Handler h, int what, Object obj);
public static Message obtain(Handler h, int what, int arg1, int arg2);
public static Message obtain(Handler h, int what,
            int arg1, int arg2, Object obj);

那没有传参数的Message是怎么拿到的handler的呢?我也不知道,只从注释中发现呢这一段话,等待探索

each Handler has its own name-space for message codes, so you do not need to worry about yours conflicting with other handlers.

截图

??正文结束??
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值