user3478418
user3478418

Reputation: 21

Show progress dialog while DownloadManager is downloading?

I have a download manager that downloads multiple files. How can I get a progress dialog to show up when the manager starts the first request and close the dialog once the last request has finished downloading?

Im guessing I would need to check using ACTION_DOWNLOAD_COMPLETE but Im not sure how to implement it. Thanks for helping.

Upvotes: 2

Views: 2254

Answers (1)

Luke Sleeman
Luke Sleeman

Reputation: 1646

Here is some utility code I wrote to perform a download using DownloadManager and pass the results to a callback

public static void downloadPdf(Context context, String url, String name, final Callback callback){
    Uri uri;
    try{
        uri = Uri.parse(url);
    }
    catch(Exception e){
        Log.e(LogTags.UI, "Error parsing pdf url " + url, e);
        callback.onError();
        return;
    }

    DownloadManager.Request r = new DownloadManager.Request(uri);

    // This put the download in the same Download dir the browser uses
    r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name);

    // When downloading music and videos they will be listed in the player
    // (Seems to be available since Honeycomb only)
    r.allowScanningByMediaScanner();

    // Notify user when download is completed
    // (Seems to be available since Honeycomb only)
    r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

    // Start download
    final DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
    final long downloadId = dm.enqueue(r);

    BroadcastReceiver receiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                if(intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0) != downloadId){
                    // Quick exit - its not our download!
                    return;
                }

                DownloadManager.Query query = new DownloadManager.Query();
                query.setFilterById(downloadId);
                Cursor c = dm.query(query);
                if (c.moveToFirst()) {
                    int columnIndex = c
                            .getColumnIndex(DownloadManager.COLUMN_STATUS);
                    if (DownloadManager.STATUS_SUCCESSFUL == c
                            .getInt(columnIndex)) {

                        String uriString = c
                                .getString(c
                                        .getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                        Uri uri = Uri.parse(uriString);

                        intent = new Intent();
                        intent.setAction(Intent.ACTION_VIEW);
                        intent.setDataAndType(uri, "application/pdf");

                        callback.onSuccess();

                        context.startActivity(intent);

                    }
                    else{
                        callback.onError();
                    }
                }
                else{
                    callback.onError();
                }
                context.unregisterReceiver(this);
            }
        }
    };

    context.registerReceiver(receiver, new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}

You can perform whatever UI work you want in the callback - in my case I start an indeterminate progress dialog before starting the download then hide it on complete:

    String filename =  "Some Notes.pdf";
    final ProgressDialog dialog = ProgressDialog.show(this,
            "Downloading PDF",
            "Donwloading " + filename, true, true);

   downloadPdf(this,
            "http://some-url.com/with_pdf.pdf",
            filename,
            new Callback() {
                @Override
                public void onSuccess() {
                    dialog.dismiss();
                }

                @Override
                public void onError() {
                    dialog.dismiss();
                }
            });

PS. I'm using the Callback interface from the helpful Picasso library - You could easily define your own, it only has the two onSuccess and onError methods.

Upvotes: 4

Related Questions