简体   繁体   中英

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM