2

I would like to download multiple files with the DownloadManager on Android. I would like to see only 1 notification on the notification area with a ProgressBar which will show me the whole downloading progress state. Is it possible?

For example there are 2 files, the 1st is 1MB and the 2nd is 3Mb. When DownloadManager downloaded 1MB, then the progress state is 25%.

xarlymg89
  • 2,552
  • 2
  • 27
  • 41
csbako
  • 181
  • 1
  • 7

1 Answers1

0

The behavior you are describing is already available in the DownloadManager by default. You only need to enqueue several downloads, and the Android OS will automatically show to you what's the progress of all the enqueued downloads.

To enqueue several downloads you will have to do something similar to this:

val url = "http://your-file1.extension"
val request = DownloadManager.Request(Uri.parse(url))
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
request.setDestinationInExternalPublicDir(WALKAHOLIC_SDCARD_DIRECTORY, "your-file1.extension")

val url2 = "http://your-file2.extension"
val request2 = DownloadManager.Request(Uri.parse(url2))
request2.allowScanningByMediaScanner()
request2.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
request2.setDestinationInExternalPublicDir(YOUR_DESIRED_DOWNLOAD_DIRECTORY, "your-file2.extension")

val manager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadId1 = manager.enqueue(request)
val downloadId2 = manager.enqueue(request2)

As you can see, I have created 2 requests (one for each of the downloads) and I have configured the notification visibility:

setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)

Aside from that, both requests have been enqueued to the DownloadManager.

It could be that in previous versions of Android, the downloads were being displayed separately. But at least in Android 7, 8 and 9 are being shown to me in a combined way. So you can see the combined progress of all the downloads that have been requested.

xarlymg89
  • 2,552
  • 2
  • 27
  • 41