[英]Java/android how to start an AsyncTask after 3 seconds of delay?
如何在延迟 3 秒后启动 AsyncTask?
Using handlers as suggested in the other answers, the actual code is:使用其他答案中建议的处理程序,实际代码是:
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
new MyAsyncTask().execute();
}
}, 3000);
You can use Handler for that.您可以为此使用 Handler。 Use postDelayed(Runnable, long) for that.
为此使用 postDelayed(Runnable, long) 。
Handler#postDelayed(Runnable, Long) 处理程序#postDelayed(Runnable, Long)
You can use this piece of code to run after a 3 sec delay.您可以使用这段代码在 3 秒延迟后运行。
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// run AsyncTask here.
}
}, 3000);
使用 Handler 类,并定义 Runnable handleMyAsyncTask
将包含在 3000 毫秒延迟后执行的代码:
mHandler.postDelayed(handleMyAsyncTask, 1000*3);
Use CountDownTimer.使用倒数计时器。
new CountDownTimer(3000, 1000) {
public void onTick(long millisUntilFinished) {
//do task which continuously updates
}
public void onFinish() {
//Do your task
}
}.start();
3000 is total seconds and 1000 is timer tick on that time means on above case timer ticks 3 time. 3000 是总秒数,1000 是计时器滴答时间,这意味着上面的案例计时器滴答了 3 次。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.