Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 344
lacas Avatar asked Nov 14 '10 12:11

lacas


3 Answers

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);
like image 140
Facundo Olano Avatar answered Oct 19 '22 04:10

Facundo Olano


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

Handler#postDelayed(Runnable, Long)

like image 26
Juhani Avatar answered Oct 19 '22 03:10

Juhani


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);
like image 20
mani Avatar answered Oct 19 '22 04:10

mani