Skip to main content

How to enqueue multiple downloads using DownloadManager in Kotlin Android

How to enqueue multiple downloads using DownloadManager in Kotlin Android.

Here's a step-by-step tutorial on how to enqueue multiple downloads using DownloadManager in Kotlin for Android:

Step 1: Add permissions and dependencies

To use the DownloadManager API, you need to add the necessary permissions and dependencies to your Android project. Open your app's AndroidManifest.xml file and add the following permissions:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Next, open your app's build.gradle file and add the following dependency:

dependencies {
implementation 'androidx.core:core-ktx:1.5.0'
}

Step 2: Create a DownloadManager instance

In your activity or fragment, create an instance of the DownloadManager class:

val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

Step 3: Create a DownloadManager.Request for each download

To enqueue multiple downloads, you need to create a separate DownloadManager.Request object for each download. The DownloadManager.Request allows you to specify the download URL, destination file path, and other download parameters.

Here's an example of creating a DownloadManager.Request:

val request = DownloadManager.Request(Uri.parse("https://example.com/file1.pdf"))
.setTitle("File 1")
.setDescription("Downloading File 1")
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file1.pdf")

You can create multiple requests for each download and customize them as per your requirements.

Step 4: Enqueue the downloads

Once you have created the DownloadManager.Request objects, you can enqueue them using the DownloadManager's enqueue() method. This method returns a unique download ID for each download.

val downloadId1 = downloadManager.enqueue(request1)
val downloadId2 = downloadManager.enqueue(request2)

You can store these download IDs if you need to track the progress or status of each download later.

Step 5: Handle download completion

To handle download completion, you can register a BroadcastReceiver to receive the DOWNLOAD_COMPLETE action. This broadcast will be triggered when each download completes.

val downloadCompleteReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
// Handle download completion for the given download ID
}
}

registerReceiver(downloadCompleteReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))

Remember to unregister the receiver when you no longer need it:

unregisterReceiver(downloadCompleteReceiver)

Step 6: Monitor download progress (optional)

If you want to monitor the progress of each download, you can register a BroadcastReceiver to receive the ACTION_DOWNLOAD_STATUS_CHANGED action. This broadcast will be triggered when the download progress changes.

val downloadProgressReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
val query = DownloadManager.Query().setFilterById(id)
val cursor = downloadManager.query(query)

if (cursor.moveToFirst()) {
val downloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))
val totalBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
val progress = (downloadedBytes * 100 / totalBytes).toInt()
// Update progress for the given download ID
}

cursor.close()
}
}

registerReceiver(downloadProgressReceiver, IntentFilter(DownloadManager.ACTION_DOWNLOAD_STATUS_CHANGED))

Again, don't forget to unregister the receiver when you're done:

unregisterReceiver(downloadProgressReceiver)

That's it! You now know how to enqueue multiple downloads using DownloadManager in Kotlin for Android. Remember to handle any errors or exceptions that may occur during the download process.