// 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正在退出。
那么我们需要了解AttachInfo和HandlerActionQueue。
AttachInfo
AttachInfo是View的静态内部类。View关联到父window后,用这个类来存储一些信息。
AttachInfo存储的一部分信息如下:
WindowId mWindowId
window的标志View mRootView
最顶部的viewHandler 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行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!**