[英]Handler.postDelayed v/s Runnable.run. Is it alright to call .run instead of .postDelayed?
I was trying to implement a looping Runnable
. 我正在尝试实现循环的
Runnable
。 The example I've found seems to use the following idea to kick-start the runnable. 我发现的示例似乎使用以下思路来启动可运行对象。
handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
handler.postDelayed(this, 10000);
doIt();
count ++;
}
};
r.run();// what I prefer
// handler.postDelayed(r, 1000);//their idea
I prefer using the call to the run()
method to start the Runnable
. 我更喜欢使用对
run()
方法的调用来启动Runnable
。 What would be the possible troubles that I could get into if any by a direct call to run()
! 如果直接调用
run()
可能会遇到什么麻烦!
Thanks! 谢谢! :)
:)
If you call r.run()
, the Runnable
will be executed immediately. 如果调用
r.run()
,则Runnable
将立即执行。 But if you call handler.postDelayed(r, 1000);
但是如果您调用
handler.postDelayed(r, 1000);
as they recommended, the Runnable
will not be executed until 1s is past. 按照他们的建议,
Runnable
直到1s才执行。 So, this is the first point. 因此,这是第一点。 Second, why don't you use the
Thread Loop merchanism
provided by Android
, namely Handler
AsyncTask
HandlerThread
for the sake that Android
supports it very well? 其次,为什么不使用
Android
提供的Thread Loop merchanism
,即Handler
AsyncTask
HandlerThread
,以确保Android
很好地支持它呢?
I prefer using the call to the run() method to start the Runnable.
我更喜欢使用对run()方法的调用来启动Runnable。
OK. 好。
What would be the possible troubles that I could get into if any by a direct call to run()!
如果直接调用run()可能会遇到什么麻烦!
The first pass through run()
would happen immediately, as opposed to your commented-out code, which would cause the first pass through run()
to occur ~1000ms from now. 与您注释掉的代码相反,第一次通过
run()
会立即发生,这将导致从现在开始第一次通过run()
发生〜1000ms。
However, I would dump the Handler
. 但是,我将转储
Handler
。 postDelayed()
is also a method on View
, so just use some widget in your UI. postDelayed()
也是View
一种方法,因此只需在UI中使用一些小部件即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.