The Android is a system service designed to handle long-running HTTP downloads in the background, even if the user exits your application . To perform an action when a download finishes—such as opening a file or updating a UI element—you must implement a Download Complete Listener using a BroadcastReceiver . 1. How the "Listener" Works
Android does not provide a standard interface-style listener (like an OnClickListener ) for DownloadManager . Instead, the system sends a with the action ACTION_DOWNLOAD_COMPLETE whenever any download finishes. Your app "listens" by registering a receiver that catches this specific intent. 2. Implementation Steps
Define a receiver that extracts the download ID from the incoming intent. Compare it with your stored downloadId to ensure the notification belongs to your specific request. android download manager complete listener
private val onDownloadComplete = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent?) { val id = intent?.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1) if (id == downloadId) { // Your download is complete! checkDownloadStatus(id) } } } Use code with caution. C. Register and Unregister
val downloadManager = getSystemService(DOWNLOAD_SERVICE) as DownloadManager val request = DownloadManager.Request(Uri.parse(url)) // ... configure request ... val downloadId = downloadManager.enqueue(request) // Store this ID Use code with caution. B. Create the BroadcastReceiver The Android is a system service designed to
Register the receiver in your activity or service to start listening. It is critical to it (typically in onDestroy ) to avoid memory leaks if the activity is closed while a download is still pending.
When you enqueue a download request, the DownloadManager returns a unique . You must store this ID to verify later which download has actually finished. How the "Listener" Works Android does not provide
To successfully listen for a download completion, follow these core steps: A. Initialize the Download