Skip to main content

How to handle download failures and retries using DownloadManager in Kotlin Android

How to handle download failures and retries using DownloadManager in Kotlin Android.

Here's a step-by-step tutorial on how to handle download failures and retries using DownloadManager in Kotlin for Android.

Step 1: Add permissions to the manifest

To use the DownloadManager, you need to add the necessary permissions to your AndroidManifest.xml file. Open the file and add the following permissions:

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

Step 2: Create a DownloadManager instance

In your Kotlin activity or fragment, create an instance of DownloadManager. You can do this by calling getSystemService() with the DOWNLOAD_SERVICE constant.

val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

Step 3: Create a download request

To initiate a download, you need to create a DownloadManager.Request object. This object allows you to specify the URL of the file you want to download, as well as other options like the destination directory and notification visibility.

val uri = Uri.parse("http://example.com/file-to-download")
val request = DownloadManager.Request(uri)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename.ext")

Step 4: Enqueue the download request

To start the download, you need to enqueue the download request using the DownloadManager. This will add the request to the download queue and return a download ID, which you can use to track the progress or retrieve information about the download.

val downloadId = downloadManager.enqueue(request)

Step 5: Handle download completion

To handle the completion of a download, you can register a BroadcastReceiver to listen for the ACTION_DOWNLOAD_COMPLETE intent. This intent will be broadcasted by the system when a download is completed.

val onComplete = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
if (id == downloadId) {
// Handle download completion
}
}
}
registerReceiver(onComplete, IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE))

Step 6: Handle download failures and retries

To handle download failures and retries, you can listen for the ACTION_DOWNLOAD_STATUS intent. This intent will be broadcasted by the system when the status of a download changes. You can check the status of the download and take appropriate actions, such as retrying the download.

val onStatus = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
val status = intent?.getIntExtra(DownloadManager.EXTRA_DOWNLOAD_STATUS, -1)
if (id == downloadId && status == DownloadManager.STATUS_FAILED) {
// Handle download failure
// Retry the download if needed
val retryRequest = DownloadManager.Request(uri)
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "filename.ext")
val retryId = downloadManager.enqueue(retryRequest)
}
}
}
registerReceiver(onStatus, IntentFilter(DownloadManager.ACTION_DOWNLOAD_STATUS))

That's it! You have now learned how to handle download failures and retries using the DownloadManager in Kotlin for Android. You can customize the handling of failures and retries based on your specific requirements.