简体   繁体   English

关于Android中的处理程序的问题

[英]A question about Handlers in Android

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() someOtherMethod()是否会在someMethod()之后运行

No it won't. 不,不会。 The second one will be performed immediately when all pending handler requests (so on the UI thread) are done. 当所有待处理的处理程序请求(在UI线程上)完成时,将立即执行第二个。 The first will be appended after 10 seconds. 第一个将在10秒后追加。

Hint: The handler takes a Runnable , so it looks like that: 提示:处理程序采用Runnable ,因此它看起来像这样:

handler.post(new Runnable {
    public void run() {
        doSomething();
    }
}

Update based on comment: 根据评论更新

The handler invokes the posted runnable on the thread on which new Handler() was called. 处理程序在调用new Handler()的线程上调用发布的可运行对象。

否,第二篇文章将尽快(在第一篇之前)运行。

No, handler posts are asynchronous. 不,处理程序帖子是异步的。 what handler.post() does it that it adds the runnable to the message queue. handler.post()作用是将可运行对象添加到消息队列中。 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. handler.postDelayed()立即将runnable添加到消息队列中,但是计时器将设置为delay ms,之后将执行runnable。

so all post does is , to add the runnable to the message queue. 因此,所有发布的内容是,将可运行对象添加到消息队列中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM