Java/android how to start an AsyncTask after 3 seconds of delay?
How can an AsyncTask be started after a 3 se开发者_如何学Gocond delay?
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. Use postDelayed(Runnable, long) for that.
Handler#postDelayed(Runnable, Long)
You can use this piece of code to run after a 3 sec delay.
new Timer().schedule(new TimerTask() {
@Override
public void run() {
// run AsyncTask here.
}
}, 3000);
Use Handler class, and define Runnable handleMyAsyncTask
that will contain code executed after 3000 msec delay:
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.
精彩评论