How to stop AsyncTask timing out if it's progressing (even if slowly)

89 views Asked by At

My Android app makes three distinct forms of data transfer:

  1. Queries the local database: returns results if found (local / short timeout);
  2. Queries a remote API: typical response size is around 1k (remote but tiny payload / medium timeout);
  3. Downloads a remote file: size may be anywhere between 100k - 1MB (remote and large data / long timeout).

These transfers are made using extended subclasses of AsyncTask and they work pretty well, certainly for steps 1 and 2. The issue is with step 3.

If the device is on WiFi, 3G or another reasonably fast connection, the AsyncTask will not time out and the remote file downloads in time. If the device is on 2G or a slow connection, it will time out - even when the download is still progressing (albeit slowly).

Ideally, I would like to monitor the download progress at intervals, and either reset the AsyncTask's internal timeout counter or prevent the task from cancelling if progress has been made since the last check. Unfortunately, both of those options seem to require either read or write access to AsyncTask's private timeout variable.

How can I prevent AsyncTask from timing out when download progress is still continuing?

Edit: I've just noticed that StackOverflow offered me the tag of Android Download Manager. Is this the kind of thing that ADM is designed for?

1

There are 1 answers

0
Cosmittus On

I'm going to answer my own question here, or at least the edit, to say that in the end I implemented Android's DownloadManager to ensure that background downloads could continue at whatever pace the connection allowed. I then implemented a Manifest-declared Receiver which started a Service to insert the response into the database. Finally, the user is notified via the toolbar that the download has finished and that they may return to the app to view the results.

However, I'm not going to accept my own answer :-) because I haven't answered whether it's possible to stop an AsyncTask timing out in the way that I asked. But everything I found suggests that the answer is 'no'.