Skip to content Skip to sidebar Skip to footer

Java/android How To Start An Asynctask After 3 Seconds Of Delay?

How can an AsyncTask be started after a 3 second delay?

Solution 1:

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);

Solution 2:

You can use Handler for that. Use postDelayed(Runnable, long) for that.

Handler#postDelayed(Runnable, Long)

Solution 3:

You can use this piece of code to run after a 3 sec delay.

newTimer().schedule(newTimerTask() {          
    @Overridepublicvoidrun() {

        // run AsyncTask here.    


    }
}, 3000);

Solution 4:

Use Handler class, and define Runnable handleMyAsyncTask that will contain code executed after 3000 msec delay:

mHandler.postDelayed(handleMyAsyncTask, 1000*3);

Solution 5:

Use CountDownTimer.

new CountDownTimer(3000, 1000) {

        publicvoidonTick(long millisUntilFinished) {

           //do task which continuously updates

        }

        publicvoidonFinish() {

           //Do your task
         
        }

    }.start();

3000 is total seconds and 1000 is timer tick on that time means on above case timer ticks 3 time.

Post a Comment for "Java/android How To Start An Asynctask After 3 Seconds Of Delay?"