I'm downloading an image from my server using the Download Manager
.
It downloads the file fine and puts it where I want it to. But for some reason the notification sticks and I can't seem to remove it. The code for the download manager is as follows:
mDownloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
Uri uri = Uri.parse("URL"));
long enqueue = mDownloadManager.enqueue(new DownloadManager.Request(uri)
.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI)
.setAllowedOverRoaming(false)
.setTitle("Title")
.setDescription("File description")
.setDestinationInExternalPublicDir("Folder", "Filename")
.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE));
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Toast.makeText(getApplicationContext(), "Download Completed", Toast.LENGTH_SHORT).show();
}
};
How do I remove the notification once it has been downloaded?.
I have tried setting all the different notification visibility modes with no luck. Is there something I can do from the BroadcastReceiver once it is finished?
I managed to work out my problem. In the BroadcastReceiver
I had to get the download id from the intent and remove that from the DownloadManager
.
BroadcastReceiver onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
Toast.makeText(getApplicationContext(), "Download Completed", Toast.LENGTH_SHORT).show();
// Get the download_id of the completed download.
long download_id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
// Remove the completed download from the DownloadManager
mDownloadManager.remove(download_id);
}
};
I also want to note that by doing mDownloadManager.remove(download_id)
, this will delete the file from memory. I had to add additional code to save the file permanently in the location I wanted it to save originally.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With