使用下载管理器内的活性显示下载进度 [英] Show Download progress inside activity using DownloadManager

查看:102
本文介绍了使用下载管理器内的活性显示下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重现相同的进步下载管理器显示在我的应用程序通知栏,但我的进步永远不会被发表。我想它使用runOnUiThread()来更新,但由于某些原因,它没有被更新。

I'm trying to reproduce the same progress that DownloadManager shows in Notification bar inside my app, but my progress never is published. I'm trying to update it using runOnUiThread(), but for some reason it's not been updated.

我的下载:

String urlDownload = "https://dl.dropbox.com/s/ex4clsfmiu142dy/test.zip?token_hash=AAGD-XcBL8C3flflkmxjbzdr7_2W_i6CZ_3rM5zQpUCYaw&dl=1";
        DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload));

        request.setDescription("Testando");
        request.setTitle("Download");
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip");

        final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);

        final long downloadId = manager.enqueue(request);

        final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1);

        new Thread(new Runnable() {

            @Override
            public void run() {

                boolean downloading = true;

                while (downloading) {

                    DownloadManager.Query q = new DownloadManager.Query();
                    q.setFilterById(downloadId);

                    Cursor cursor = manager.query(q);
                    cursor.moveToFirst();
                    int bytes_downloaded = cursor.getInt(cursor
                            .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                    int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                    if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                        downloading = false;
                    }

                    final double dl_progress = (bytes_downloaded / bytes_total) * 100;

                    runOnUiThread(new Runnable() {

                        @Override
                        public void run() {

                            mProgressBar.setProgress((int) dl_progress);

                        }
                    });

                    Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor));
                    cursor.close();
                }

            }
        }).start();

我statusMessage方式:

my statusMessage method:

private String statusMessage(Cursor c) {
    String msg = "???";

    switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
    case DownloadManager.STATUS_FAILED:
        msg = "Download failed!";
        break;

    case DownloadManager.STATUS_PAUSED:
        msg = "Download paused!";
        break;

    case DownloadManager.STATUS_PENDING:
        msg = "Download pending!";
        break;

    case DownloadManager.STATUS_RUNNING:
        msg = "Download in progress!";
        break;

    case DownloadManager.STATUS_SUCCESSFUL:
        msg = "Download complete!";
        break;

    default:
        msg = "Download is nowhere in sight";
        break;
    }

    return (msg);
}

我的日志工作的很好,而我的下载运行时表示,正在下载!而当它是完整的下载完成!,但同样不会发生在我的进步,为什么呢?我真的需要一些帮助,其他的逻辑做的,真的是AP preciated

推荐答案

正在分两个整数:

final double dl_progress = (bytes_downloaded / bytes_total) * 100;

由于 bytes_downloaded 小于 bytes_total (bytes_downloaded / bytes_total)为0,因此,你的进步将始终为0。

As bytes_downloaded is less than bytes_total, (bytes_downloaded / bytes_total) will be 0, and your progress will therefore always be 0.

更改计算

final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

要取得进展,整个(虽然地板)百分。

to obtain the progress in whole (albeit floored) percentiles.

这篇关于使用下载管理器内的活性显示下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆