Skip to main content

How to restrict download to Wi-Fi only using DownloadManager in Kotlin Android

How to restrict download to Wi-Fi only using DownloadManager in Kotlin Android.

In this tutorial, we will learn how to restrict downloads to Wi-Fi only using the DownloadManager in Kotlin Android. The DownloadManager class provides a way to handle long-running downloads in the background, and by default, it allows downloads to occur over both Wi-Fi and mobile data. However, if you want to restrict downloads to Wi-Fi only, you can follow the steps outlined below.

Step 1: Add necessary permissions to the manifest

First, we need to add the necessary permissions to the AndroidManifest.xml file. Open the file and add the following permissions:

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

The INTERNET permission allows the app to access the internet, while the ACCESS_NETWORK_STATE permission allows the app to check the network connectivity status.

Step 2: Create a BroadcastReceiver

Next, we need to create a BroadcastReceiver to listen for network connectivity changes. Create a new Kotlin file called NetworkChangeReceiver.kt and add the following code:

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.net.NetworkInfo

class NetworkChangeReceiver : BroadcastReceiver() {

override fun onReceive(context: Context?, intent: Intent?) {
val connectivityManager =
context?.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val networkInfo: NetworkInfo? = connectivityManager.activeNetworkInfo

if (networkInfo != null && networkInfo.isConnected) {
val networkType = networkInfo.type
if (networkType == ConnectivityManager.TYPE_WIFI) {
// Wi-Fi is connected, allow downloads
// TODO: Implement your download logic here
}
} else {
// No network connection, pause or cancel ongoing downloads
// TODO: Implement your pause or cancel download logic here
}
}
}

This BroadcastReceiver listens for network connectivity changes and checks if the device is connected to Wi-Fi. You can implement your download logic to start, pause, or cancel downloads based on the network connectivity status.

Step 3: Register the BroadcastReceiver

To receive network connectivity change events, we need to register the BroadcastReceiver in the AndroidManifest.xml file. Open the file and add the following code within the <application> tag:

<receiver
android:name=".NetworkChangeReceiver"
android:enabled="true"
android:exported="true">
<intent-filter>
<action android:name="android.net.conn.CONNECTIVITY_CHANGE" />
</intent-filter>
</receiver>

This registers the NetworkChangeReceiver to receive connectivity change events.

Step 4: Download files using DownloadManager

To download files using the DownloadManager, you can use the following code snippet:

val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager

val request = DownloadManager.Request(Uri.parse("YOUR_DOWNLOAD_URL"))
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
request.setTitle("Your download title")
request.setDescription("Your download description")
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "your_file_name")

val downloadId = downloadManager.enqueue(request)

In this code, we create a new DownloadManager.Request and set the allowed network type to NETWORK_WIFI, which restricts the download to Wi-Fi only. You can customize other properties of the request, such as the download URL, title, description, and destination file path.

Finally, we enqueue the download request using downloadManager.enqueue(request), which returns a unique download ID that can be used to track the download progress or perform other operations.

Conclusion

In this tutorial, we learned how to restrict downloads to Wi-Fi only using the DownloadManager in Kotlin Android. We created a BroadcastReceiver to listen for network connectivity changes and implemented the necessary logic to start, pause, or cancel downloads based on the network status. By setting the allowed network type to NETWORK_WIFI in the DownloadManager request, we ensured that downloads only occur when the device is connected to Wi-Fi.