Gedanggoreng
Gedanggoreng

Reputation: 321

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.

Upvotes: 0

Views: 3714

Answers (2)

Gedanggoreng
Gedanggoreng

Reputation: 321

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.

 @Override
 protected void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 ...

        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);
            //Registering receiver in Download Manager
            registerReceiver(onCompleted, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));    
            downloadManager.enqueue(request);
            Toast.makeText(getApplicationContext(), "Downloading File", Toast.LENGTH_SHORT).show();
        }
    });

 ...

 BroadcastReceiver onCompleted = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        Toast.makeText(context.getApplicationContext(), "Download Finish", Toast.LENGTH_SHORT).show();

Upvotes: 0

a7d.24_
a7d.24_

Reputation: 300

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" />

Upvotes: 1

Related Questions