开发者

Java/android how to start an AsyncTask after 3 seconds of delay?

开发者 https://www.devze.com 2023-01-25 04:51 出处:网络
How can an AsyncTask be started after a 3 se开发者_如何学Gocond delay?Using handlers as suggested in the other answers, the actual code is:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号