1

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

abhishek
  • 1,434
  • 7
  • 39
  • 71

2 Answers2

4

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.

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
4

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
}
firefox1986
  • 1,602
  • 11
  • 9