Jake Wilson
Jake Wilson

Reputation: 91193

Android - How to handle AsyncTask timeouts?

In my app, I use AsyncTask classes to make HTTP connections and download information to display.

Sometimes, the amount of data is substantial and takes some time. If a user and their phone happens to be in a place with slow/poor/spotty reception, there might be HTTP timeouts or maybe they loose connectivity all together, etc...

What is the best way to handle these timeouts gracefully?

I've been expirimenting with creating a Runnable handler that runs XX seconds after starting the AsyncTask. The Runnable would simply test to see if the AsyncTask is still running and if so, assume the data connection is too slow and go ahead and kill the thread and gracefully give an error message about the data connection.

Is this a good approach or is there a more practical method?

Upvotes: 2

Views: 724

Answers (1)

Stephen
Stephen

Reputation: 8178

It's been a while since I've programmed for Android, but I know that AsyncTask has an update event (to track download percentages and such). You could probably use that to have it check itself.

Upvotes: 1

Related Questions