I have a download feature in my android app. When I download a file, it will notify me by toast. I want to know how to have a toast notification when the download complete. I have trying other suggestions but haven’t succeed in finding the result I expected.
Here is the download code.
webView.setDownloadListener(new DownloadListener() {
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition,
String mimeType, long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
request.setMimeType(mimeType);
String cookies = CookieManager.getInstance().getCookie(url);
request.addRequestHeader("cookie",cookies);
request.addRequestHeader("User-Agent",userAgent);
request.setDescription("Downloading File");
request.setTitle(URLUtil.guessFileName(url,contentDisposition,mimeType));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,
URLUtil.guessFileName(url, contentDisposition, mimeType));
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);
Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_SHORT).show();
}
});
Thank you in advance.
2
Answers
Thanks to @a7d.24_ for the answer given.
I have simplified the answer so I didn't need to create new Java Class (OnComplete) and therefore no need to add it to the Manifest.XML.
I just have to add single line of code inside webView.setDownloadListener and create new BroadcastReceiver in the WebView Class.
You can create a
Broadcast Receiver
to handle any file that had completed downloading :Main Activity
The Broadcast Receiver (OnComplete)
and please don’t forget to add this in your
Mainfest.xml
at the<application/>
tag