Skip to main content

Android DownloadManager

Introduction and how to use DownloadManager in Kotlin Android to download files from the internet.

Introduction

DownloadManager for Android is a system service provided by the Android operating system that enables users to download files from the internet. It offers a convenient way to manage and keep track of all downloads on the device. With DownloadManager, users can initiate downloads, pause and resume them, and even prioritize them as per their preference.

It also handles the necessary network connectivity and file handling, ensuring a smooth and efficient downloading experience. Additionally, DownloadManager provides notifications and callbacks to keep users informed about the progress and completion of their downloads. It is a powerful tool that simplifies and enhances the downloading process for Android users.

Download Files from Internet

Here's a step-by-step tutorial on how to use DownloadManager in Kotlin Android to download files from the internet:

Step 1: Add the necessary permissions to your AndroidManifest.xml file:

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

These permissions are required to access the internet and write files to external storage.

Step 2: Create a new activity or fragment where you want to implement the file download functionality.

Step 3: Import the required classes:

import android.app.DownloadManager
import android.content.Context
import android.net.Uri
import android.os.Environment

These classes are needed for using the DownloadManager.

Step 4: Initialize the DownloadManager in your activity or fragment:

val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

Step 5: Create a function to start the file download:

fun startDownload(url: String, fileName: String) {
val request = DownloadManager.Request(Uri.parse(url))
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI or DownloadManager.Request.NETWORK_MOBILE)
.setAllowedOverRoaming(false)
.setTitle(fileName)
.setDescription("Downloading")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName)

downloadManager.enqueue(request)
}

This function takes the URL of the file to download and the desired file name as parameters. It creates a DownloadManager.Request object and sets various properties like network types, title, description, notification visibility, and destination directory.

Step 6: Call the startDownload function with the appropriate URL and file name to initiate the download:

val fileUrl = "https://example.com/file.pdf"
val fileName = "example.pdf"
startDownload(fileUrl, fileName)

Step 7: (Optional) Handle the download completion:

val onCompleteReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
// Handle download completion here
}
}

val intentFilter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
registerReceiver(onCompleteReceiver, intentFilter)

This code registers a BroadcastReceiver to handle the completion of the download. You can add your own logic inside the onReceive method to perform any required actions when the download is complete.

Step 8: (Optional) Clean up the BroadcastReceiver:

override fun onDestroy() {
super.onDestroy()
unregisterReceiver(onCompleteReceiver)
}

It's important to unregister the BroadcastReceiver to avoid any memory leaks. Call the unregisterReceiver method in the onDestroy method of your activity or fragment.

That's it! You now have a working implementation of DownloadManager in Kotlin Android to download files from the internet. You can customize the download request further by exploring the available methods in the DownloadManager.Request class.