关于Android中Handler的一个问题
如果我理解正确的话,处理程序就像队列一样工作。所以我的简单问题是我是否先延迟发布,然后再定期发布。第二个帖子会在第一个延迟的帖子之后运行吗?
handler.postDelayed(someMethod(), 10000);
handler.post(someOtherMethod());
someOtherMethod() 会在 someMethod() 之后运行吗
If I've got it right a handler works like a queue. So my simple question is if I first postDelayed and after that do I regular post. Will the second post run after the first delayed post?
handler.postDelayed(someMethod(), 10000);
handler.post(someOtherMethod());
Will the someOtherMethod() run after someMethod()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不,不会。当所有挂起的处理程序请求(因此在 UI 线程上)完成后,第二个请求将立即执行。第一个将在 10 秒后添加。
提示:处理程序采用一个 Runnable,因此它看起来像这样:
基于注释更新:
处理程序在
new Handler( )
被调用。No it won't. The second one will be performed immediately when all pending handler requests (so on the UI thread) are done. The first will be appended after 10 seconds.
Hint: The handler takes a
Runnable
, so it looks like that:Update based on comment:
The handler invokes the posted runnable on the thread on which
new Handler()
was called.不,第二篇文章将尽快发布(在第一篇文章之前)。
No, 2nd post will run as soon as possible (before 1st).
不,处理程序帖子是异步的。 handler.post() 的作用是将可运行对象添加到消息队列中。 handler.postDelayed() 立即将 runnable 添加到消息队列,但计时器将设置为延迟 ms,之后 runnable 将被执行。
所以所有 post 所做的就是将可运行对象添加到消息队列中。
No, handler posts are asynchronous. what
handler.post()
does it that it adds the runnable to the message queue.handler.postDelayed()
adds the runnable to the message queue immediately , but the timer will be set to delayed ms , after which the runnable will be executed.so all post does is , to add the runnable to the message queue.