stop async task after 60 second

隐身守侯 提交于 2019-12-23 02:57:19

问题


i want to stop async task after 60 second

i know there is a method get(time , timeunit) which i should be using in doInBackground but i dont know how to check if the time out has reached or not please provide that sample one line code .

Thanks


回答1:


If I understand your question correctly, you already know how to abandon the task after n seconds, but you need to know IF it was abandoned?

According to AsyncTask docs it'll throw a TimeoutException.

Do something like:

try {
    //your AsyncTask code
} catch(TimeoutException ex) {
    //your timeout code
}



回答2:


you can use a TimerTask to which you can start in the doInBackround() with the schedule for 60 seconds. in the run() of the TimerTask just call the stop for the async task.



来源:https://stackoverflow.com/questions/5417741/stop-async-task-after-60-second

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