General Direction of this Question
For this question, think of the job management that a web browser needs to do for downloading websites, images, and possibly other content for displaying to the user. Technically, this is a very similar scenario to what I'm facing. I'm asking for the choices I have (AsyncTask
, Service
, 3rd party libraries, ...) for implementing such a job management.
Details
I'm developing an app for android which needs to download stuff in the background and update the UI whenever more data is available from the running download.
When the user chooses to, he navigates to another Activity
of his desire. The previously downloaded data then is unimportant to the new Activity
. Hence the first download needs to be stopped (to spare bandwidth for a new download) and the new download for the new Activity
needs to be started. Typical download times will vary between few seconds and half a minute to complete.
When the user navigates back to the first Activity
, the first download shall not start from scratch. Instead, it shall be resumed.
When a download for the current Activity
finishes, but there were other recent downloads which did not complete, they shall be continued so that in case the user navigates to those other activities which need that data, it can be presented immediately. Downloads shall be continued even if the app goes to background, or even regularly, say, once a day, so the app is ready to present reasonably up to date content even when offline.
What choices do I have for such a job management at API level 15? Keep in mind that it's about the job management / scheduling, not the downloading. Downloading stuff is rather easy to program, but scheduling is the difficult part.
Summary of requirements:
- cancel/suspend jobs (hence cancel downloads)
- observer gets notified on job progress
- data must be accessible in parts
- resumable jobs (read: resumable downloads; creating a new job for resuming a download is ok, of course)
- jobs can run while app is not in foreground
Choices I thought about
AsyncTask
I think it's not suitable, because it is bound to the Activity, so it's a bug to have this run in the background while the app is not in the foreground.
IntentService
According to Asking an IntentService for information about its queue it is impossible to alter an IntentService's queue.
Service
So far, I think I need to rewrite a scheduling mechanism from scratch, or base it on the source code of IntentService
like suggested by the above SO answer.