大家都知道每个View都有一个post()和postDelayed()方法,那么这两个方法是做什么用呢?什么时候需要用呢?
我们带着这两个问题来分析一下:
首先我们看下这两个方法的源码,首先看post():
/**
* Causes the Runnable to be added to the message queue.
* The runnable will be run on the user interface thread.
*
* @param action The Runnable that will be executed.
*
* @return Returns true if the Runnable was successfully placed in to the
* message queue. Returns false on failure, usually because the
* looper processing the message queue is exiting.
*
* @see #postDelayed
* @see #removeCallbacks
*/
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;
}
从源码中我们很清楚的看到,两种情况:
1.当mAttachInfo != null 时,post方法最终是由attachInfo中的mHandler调用post来处理,从而保证在ui线程中执行,所以从根本上来之后的整个流程还是Handler 的整个处理机制流程(Handler的处理机制流程,这里不做分析),那么mAttachInfo又是什么时候赋值的呢?搜索一下源码看到:
/**
* @param info the {@link android.view.View.AttachInfo} to associated with
* this view
*/
void dispatchAttachedToWindow(AttachInfo info, int visibility) {
mAttachInfo = info;
if (mOverlay != null) {
mOverlay.getOverlayView().dispatchAttachedToWindow(info, visibility);
}
可以看到mAttachInfo是在,dispatchAttachedToWindow中调用的,而dispatchAttachedToWindow 是在 ViewRootImpl 类的performTraversals 调用的,而这个方法在view初始化的时候会被调用。
2.mAttachInfo==null时,调用getRunQueue().post(action),我们来看下这个getRunQueue()的源码:
/**
* Returns the queue of runnable for this view.
*
* @return the queue of runnables for this view
*/
private HandlerActionQueue getRunQueue() {
if (mRunQueue == null) {
mRunQueue = new HandlerActionQueue();
}
return mRunQueue;
}
这里看到最终调用的是HandlerActionQueue的post()方法:
public void post(Runnable action) {
postDelayed(action, 0);
}
public void postDelayed(Runnable action, long delayMillis) {
final HandlerAction handlerAction = new HandlerAction(action, delayMillis);
synchronized (this) {
if (mActions == null) {
mActions = new HandlerAction[4];
}
mActions = GrowingArrayUtils.append(mActions, mCount, handlerAction);
mCount++;
}
}
这里我们看到基本上就是通过HandlerActionQueue缓存起来,缓存起来什么时候执行呢?我们找下HandlerActionQueue的源码里:
public void executeActions(Handler handler) {
synchronized (this) {
final HandlerAction[] actions = mActions;
for (int i = 0, count = mCount; i < count; i++) {
final HandlerAction handlerAction = actions[i];
handler.postDelayed(handlerAction.action, handlerAction.delay);
}
mActions = null;
mCount = 0;
}
}
缓存起来的runnable最终还是通过handler来执行,直接找到执行的代码:
还是通过dispatchAttachedToWindow来执行。
这两个方法:
当打开一个activity时如果调用post方法时还没有开始执行dispatchAttachedToWindow就先调用getRunQueue().post(action)缓存起来,当执行到dispatchAttachedToWindow时再执行handler的post,如果到达就直接执行handler.post。
总结:post()和postDelayed()用来更新ui,postDelayed可以设置延时。