Java/android 如何在延迟 3 秒后启动 AsyncTask?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4177409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java/android how to start an AsyncTask after 3 seconds of delay?
提问by lacas
How can an AsyncTask be started after a 3 second delay?
如何在延迟 3 秒后启动 AsyncTask?
采纳答案by Juhani
You can use Handler for that. Use postDelayed(Runnable, long) for that.
您可以为此使用 Handler。为此使用 postDelayed(Runnable, long) 。
回答by Zelimir
Use Handler class, and define Runnable handleMyAsyncTask
that will contain code executed after 3000 msec delay:
使用 Handler 类,并定义 RunnablehandleMyAsyncTask
将包含在 3000 毫秒延迟后执行的代码:
mHandler.postDelayed(handleMyAsyncTask, 1000*3);
回答by mani
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);
回答by Facundo Olano
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);