簡體   English   中英

DownloadManager正在下載時顯示進度對話框?

[英]Show progress dialog while DownloadManager is downloading?

我有一個下載管理器,可以下載多個文件。 當管理員啟動第一個請求並在最后一個請求完成下載后關閉對話框時,如何顯示進度對話框?

我想我需要使用ACTION_DOWNLOAD_COMPLETE進行檢查,但我不確定如何實現。 感謝您的幫助。

這是我編寫的一些實用程序代碼,用於使用DownloadManager執行下載並將結果傳遞給回調

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));
}

您可以在回調中執行所需的任何UI工作-就我而言,在開始下載之前,我先啟動一個不確定的進度對話框,然后將其隱藏起來:

    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。 我正在使用有用的Picasso庫中的Callback接口-您可以輕松定義自己的接口,它只有兩個onSuccess和onError方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM