Android View post 方法

本文详细分析了Android中的Handler类的post方法及其内部实现,包括消息的发送、延迟、排队以及AttachInfo和HandlerActionQueue的作用。通过源码揭示了View如何处理post任务和消息传递机制。
摘要由CSDN通过智能技术生成
    // android.os Handler 有关的部分源码
    public final boolean post(@NonNull Runnable r) {
        return sendMessageDelayed(getPostMessage(r), 0);
    }

    private static Message getPostMessage(Runnable r) {
        Message m = Message.obtain();
        m.callback = r;
        return m;
    }

    public final boolean sendMessageDelayed(@NonNull Message msg, long delayMillis) {
        if (delayMillis < 0) {
            delayMillis = 0;
        }
        return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
    }

    public boolean sendMessageAtTime(@NonNull Message msg, long uptimeMillis) {
        MessageQueue queue = mQueue;
        if (queue == null) {
            RuntimeException e = new RuntimeException(
                    this + " sendMessageAtTime() called with no mQueue");
            Log.w("Looper", e.getMessage(), e);
            return false;
        }
        return enqueueMessage(queue, msg, uptimeMillis);
    }

    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);
    }
复制代码

具体流程,可以看handler介绍

View的post方法

我们直接跟着post的源码走。

public boolean post(Runnable action) {
    final AttachInfo attachInfo = mAttachInfo;
    if (attachInfo != null) {
        return attachInfo.mHandler.post(action);
    }

    // Postpone the runnable until we know on which thread it needs to run.
    // Assume that the runnable will be successfully placed after attach.
    getRunQueue().post(action);
    return true;
}

private HandlerActionQueue getRunQueue() {
    if (mRunQueue == null) {
        mRunQueue = new HandlerActionQueue();
    }
    return mRunQueue;
}
复制代码

可以看到一开始就查询是否有attachInfo,如果有,则用attachInfo.mHandler来执行这个任务。

如果没有attachInfo,则添加到View自己的mRunQueue中。确定运行的线程后,再执行任务。

post(Runnable action)的返回boolean值,如果为true,表示任务被添加到消息队列中了。 如果是false,通常表示消息队列关联的looper正在退出。

那么我们需要了解AttachInfoHandlerActionQueue

AttachInfo

AttachInfoView的静态内部类。View关联到父window后,用这个类来存储一些信息。

AttachInfo存储的一部分信息如下:

  • WindowId mWindowId window的标志
  • View mRootView 最顶部的view
  • Handler mHandler 这个handler可以用来处理任务
HandlerActionQueue

View还没有handler的时候,拿HandlerActionQueue来缓存任务。HandlerAction是它的静态内部类,存储Runnable与延时信息。

public class HandlerActionQueue {
    private HandlerAction[] mActions;
    
    public void post(Runnable action)
    public void executeActions(Handler handler)
    // ...

    private static class HandlerAction {
        final Runnable action;
        final long delay;
        // ...
    }
}
复制代码

View的mRunQueue

将任务(runnable)排成队。当View关联上窗口并且有handler后,再执行这些任务。

/**
 * Queue of pending runnables. Used to postpone calls to post() until this


### 结语

**由于篇幅限制,文档的详解资料太全面,细节内容太多,所以只把部分知识点截图出来粗略的介绍,每个小节点里面都有更细化的内容!以下是目录截图:**

![](https://img-blog.csdnimg.cn/img_convert/cf0c038bd930da295e67e7e2f55bc885.webp?x-oss-process=image/format,png)

>由于整个文档比较全面,内容比较多,篇幅不允许,下面以截图方式展示 。
>

再附一部分Android架构面试视频讲解:

![](https://img-blog.csdnimg.cn/img_convert/9213d88101d71d079963b4785137fad0.webp?x-oss-process=image/format,png)




**网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。**

**[需要这份系统化学习资料的朋友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

友,可以戳这里获取](https://bbs.csdn.net/topics/618156601)**

**一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**

  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值