javaandroidandroid-studioandroid-download-managerdownload-manager

How To Have Notification When Download Complete


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.


Solution

  • You can create a Broadcast Receiver to handle any file that had completed downloading :

    Main Activity

    public class MainActivity extends AppCompatActivity {
    
    // create an object
    OnComplete onComplete ;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        WebView webView = null;
        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();
            }
        });
    
        // the broadcast receiver object
        onComplete = new OnComplete();
    
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        // to start handling any download that completes
        registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        // to stop handling download complete when exit app
        unregisterReceiver(onComplete);
    }
    
    }
    

    The Broadcast Receiver (OnComplete)

    import android.content.BroadcastReceiver;
    import android.content.Context;
    import android.content.Intent;
    
    public class OnComplete extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // these codes will run when download completed
        // And you can do some thing
    }
    }
    

    and please don't forget to add this in your Mainfest.xml at the <application/> tag

    <receiver android:name=".OnComplete" />