2

I need help designing a solution to a problem specific to long running background tasks.

Background:

  • I have an app with activity that displays a list of files.
  • The files can each be downloaded. Each downloaded file can have an update.
  • The updates, if any, are fired as notifications with update now button. The app has an activity that shows the updates for each file.
  • Clicking on update now in either place triggers a background job downloading the file and update the progress in UI in both the places (depending on where the user is).
  • The user can update/download a max of 4 files simultaneously.

Design:

  • I have a GCM setup with WakefulBroadcastReceiver, that starts a IntentService to notify any updates.
  • The update/download is a Runnable - DownloadRunnable.java
  • A singleton class, MyDownloadManager has a static method startDownload() that starts the DownloadRunnable with a specific url.

Problem:

  • How do I update the progress in both notifications and activity while downloading the update?
  • How do I extend the design to run concurrent downloads and update progress to the corresponding item?
Shiva
  • 697
  • 1
  • 4
  • 11

2 Answers2

3
  1. I would move the download task into a service.
  2. I would not implement it myself, instead I would use the DownloadManager System service, see doc and usage example.
  3. In your UI you can query the download status inside a background thread, see this SO question: Show Download progress inside activity using DownloadManager. Different to the solution I would use an AsyncTask instead.

This allows you to perfom downloads in parallel. In the activity you can show the current progress by polling in a background thread, but I am not sure what you mean with the notification? The DownloadManager shows a notification when starts with downloading.

Your downloads will be much stabler instead of using an own implementation, since DownloadManager can deal with connection loss and will perform a retry.

Community
  • 1
  • 1
ChrLipp
  • 15,526
  • 10
  • 75
  • 107
0

I would suggest to use Android's DownloadManager. This is the Download manager that the whole OS uses. U'll receive broadcast when the download is completed using ACTION_DOWNLOAD_COMPLETE.

Try this tutorial:

http://blog.vogella.com/2011/06/14/android-downloadmanager-example/

Naresh
  • 3,174
  • 1
  • 17
  • 22