Skip to main content

How to display download progress using DownloadManager in Kotlin Android

How to display download progress using DownloadManager in Kotlin Android.

Here's a step-by-step tutorial on how to display download progress using DownloadManager in Kotlin Android:

Step 1: Set up the project

  • Create a new Android project in Kotlin.
  • Add the necessary permissions in the AndroidManifest.xml file:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Step 2: Implement the DownloadManager

  • In your activity or fragment, create an instance of the DownloadManager:
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

Step 3: Start the download

  • To start a download, create a DownloadManager.Request object and set the necessary properties:
val request = DownloadManager.Request(Uri.parse("URL_OF_YOUR_FILE"))
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
request.setTitle("Download")
request.setDescription("Downloading file...")
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "file_name.extension")

val downloadId = downloadManager.enqueue(request)
  • Replace "URL_OF_YOUR_FILE" with the actual URL of the file you want to download.
  • You can customize the download options according to your requirements.

Step 4: Track the download progress

  • To track the download progress, you can register a BroadcastReceiver:
val receiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
val action = intent?.action
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE == action) {
val downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1)
// Handle the download completion
} else if (DownloadManager.ACTION_NOTIFICATION_CLICKED == action) {
// Handle the notification click
}
}
}

val filter = IntentFilter().apply {
addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED)
}

registerReceiver(receiver, filter)

Step 5: Display the download progress

  • To display the download progress, you can query the DownloadManager for the progress information:
val query = DownloadManager.Query().setFilterById(downloadId)
val cursor = downloadManager.query(query)

if (cursor.moveToFirst()) {
val status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS))
val totalSize = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES))
val downloadedSize = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR))

if (status == DownloadManager.STATUS_SUCCESSFUL) {
// File downloaded successfully
} else if (status == DownloadManager.STATUS_FAILED) {
// Download failed
} else {
val progress = (downloadedSize * 100L / totalSize).toInt()
// Update the UI with the progress value
}
}

cursor.close()
  • Replace "downloadId" with the actual download ID obtained in Step 3.

Step 6: Clean up

  • Unregister the BroadcastReceiver when you no longer need to track the download progress:
unregisterReceiver(receiver)

That's it! You have now learned how to display download progress using DownloadManager in Kotlin Android. Feel free to customize the code according to your specific requirements.