java - 在 DownloadManager 下载时显示进度对话框?

标签 java android android-download-manager

我有一个下载多个文件的下载管理器。如何在管理器启动第一个请求时显示进度对话框,并在最后一个请求完成下载后关闭对话框?

我想我需要使用 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();
                }
            });

附言。我正在使用有用的 Picasso 库中的回调接口(interface) - 您可以轻松定义自己的接口(interface),它只有两个 onSuccess 和 onError 方法。

关于java - 在 DownloadManager 下载时显示进度对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22747899/

相关文章:

android - 如何在单个进程中从 url 下载多个图像?

java - (Java) 如何导入和显示 Sprite ?

java - Zookeeper 未启动,nohup 错误

android - 如何删除以前在 Canvas 上的绘图?

Android 事件总线替代方案

android - 我怎样才能像vimeo app一样从后台(Android)的url下载视频

java - 在 Java 中设置游标

java - 在 Java 中通过套接字发送图像的有效方法

android - 我的 Android 版 Unity 游戏无法在 Bluestacks 中运行

android - 在 DownloadManager 中取消下载