【发布时间】:2015-10-28 22:04:34
【问题描述】:
我想知道 postDelayed(...) 方法何时执行,并且消息队列中有许多消息正在等待。在那种情况下,什么时候运行可运行对象?会在方法中定义的时间过去之后吗?或者它会等到它的角色进入消息队列?要不然是啥... ?
【问题讨论】:
标签: android runnable android-handler postdelayed
我想知道 postDelayed(...) 方法何时执行,并且消息队列中有许多消息正在等待。在那种情况下,什么时候运行可运行对象?会在方法中定义的时间过去之后吗?或者它会等到它的角色进入消息队列?要不然是啥... ?
【问题讨论】:
标签: android runnable android-handler postdelayed
让我们查看源代码和文档:
使 Runnable r 被添加到消息队列中,以便运行 经过指定的时间后。可运行的将运行 在此处理程序附加到的线程上。时基是 正常运行时间米利斯()。花在深度睡眠中的时间会增加额外的延迟 执行。
public final boolean postDelayed(Runnable r, long delayMillis) {
return sendMessageDelayed(getPostMessage(r), delayMillis);
}
现在让我们检查一下sendMessageDelayed:
在所有未决消息之后将消息排入消息队列 之前(当前时间 + delayMillis)。
public final boolean sendMessageDelayed(Message msg, long delayMillis)
{
if (delayMillis < 0) {
delayMillis = 0;
}
return sendMessageAtTime(msg, SystemClock.uptimeMillis() + delayMillis);
}
因此,postDelayed 添加要在所有待处理消息之后但在正常运行时间 + 您放置的延迟之前执行的任务。
查看此问题以获得更多解释: Does postDelayed cause the message to jump to the front of the queue?
希望对您有所帮助。
【讨论】: