Handler 的postDelayed 这方法的作用是什么?

-droidcoffee- 2011-06-24 12:02:13
public final boolean postDelayed(Runnable r, long delayMillis)


看英文文档 没看明白。。。

求解释
Causes the Runnable r to be added to the message queue, to be run
* after the specified amount of time elapses.
(前后的懂,下面这句就悲催了)
* The runnable will be run on the thread to which this handler
* is attached.


==========

网上说 每 delayMillis 执行一次, 为啥我测试一下 只执行一次?

这是为啥?
=========================

还有哪位明白人顺便把post 等该方法中的postAtTime等其他方法也解释一下

不胜感激
...全文
67453 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
arkoosa 2012-11-22
  • 打赏
  • 举报
回复
luguokankanre;
RyanJerry 2011-09-05
  • 打赏
  • 举报
回复
[Quote=引用楼主 id19870510 的回复:]
public final boolean postDelayed(Runnable r, long delayMillis)


看英文文档 没看明白。。。

求解释
Causes the Runnable r to be added to the message queue, to be run
* after the specified amount of time……
[/Quote]

该方法,是将runable对象加入消息队列。那肯定是执行一次了。除非你在runable方法体里递归调用了postDelayed方法。后面那个时间是延迟的时间,至于你翻译不懂那句,是指“该runable对象将被执行在handle所在的线程。通常是指UI线程。”
-droidcoffee- 2011-06-27
  • 打赏
  • 举报
回复
好吧, 先这样, 谢谢大家
wenzheng38 2011-06-25
  • 打赏
  • 举报
回复
LZ查看下 Handle和 Message之间的关系就了解了
Handler实际上就是个定时器,与Timer差不多,主要维护了一个消息队列MessageQueue
-droidcoffee- 2011-06-24
  • 打赏
  • 举报
回复
听楼上几位这么一解释 我好像是明白了点
prince58 2011-06-24
  • 打赏
  • 举报
回复
你测试对了,就是只执行一次。
yelloworange 2011-06-24
  • 打赏
  • 举报
回复
个人见解:
The runnable will be run on the thread to which this handler is attached.
这句话的意思是这个接口将会在线程绑定的handler上运行,就是说你的handler绑定哪个线程,就延迟哪个线程
fontlose 2011-06-24
  • 打赏
  • 举报
回复
postDelayed(Runnable r, long delayMillis)

延时delayMillis毫秒 将Runnable插入消息列队,
Runnable将在handle绑定的线程中运行。

post 是立即插入消息列队,当消息列队处理到该消息时才运行
xinqiqi123 2011-06-24
  • 打赏
  • 举报
回复
public final boolean postDelayed(Runnable r, long delayMillis)
从当前时间开始延迟delayMillis时间后执行Runnable,只执行一次


public final boolean postAtTime(Runnable r,long uptimeMillis)
在指定的有效时间内执行并且完成Runnable

源码在对此方法的返回值有个提醒:

usually because the looper processing the message queue is exiting.
Note that a
result of true does not mean the Runnable will be processed -- if
the looper is quit before the delivery time of the message
occurs then the message will be dropped.

大概意思是说结果返回true不一定就意味着执行完成了Runnable,比如-如果looper在小于uptimeMillis的时间内退出的话,消息将会被停止
xinqiqi123 2011-06-24
  • 打赏
  • 举报
回复
The runnable will be run on the thread
runnable将会在一个线程中运行

什么样的线程的?
to which this handler is attached即:
this handler is attached to the thread
有handler附属的一个线程,随便翻译的
-droidcoffee- 2011-06-24
  • 打赏
  • 举报
回复
忙了一下午,下班回家结贴

80,337

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧