使用下载管理器里面的活动显示下载进度(Show Download progress inside a

2019-08-19 07:39发布

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

我的下载:

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方法:

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

我的日志可以正常使用,而我的下载运行时表示“正在下载”! 它完成时“下载完成!”,但不会发生在我的进步一样,为什么? 我真的需要一些帮助,其他逻辑要做到这一点是非常赞赏

Answer 1:

您将两个整数:

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

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

改变你的计算

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

获得全(尽管地板)百分位数的进展。



Answer 2:

保罗的回答是正确的,但具有较大的下载,你会打到最大INT相当快,并开始得到一个负的进步。 我用这个来解决这一问题:

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


Answer 3:

正如保罗说,你将两个整数,用结果总是<1。

鸿沟,其计算和浮点返回之前, 一定要投你的电话号码。

不要忘了处理DIVBYZERO。

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


文章来源: Show Download progress inside activity using DownloadManager