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

主宰稳场 提交于 2019-12-29 05:55:39

问题


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


回答1:


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

Handler#postDelayed(Runnable, Long)




回答2:


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



回答3:


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



回答4:


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

mHandler.postDelayed(handleMyAsyncTask, 1000*3);


来源:https://stackoverflow.com/questions/4177409/java-android-how-to-start-an-asynctask-after-3-seconds-of-delay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!