带有进度对话框的 Android 下载管理器 [英] Android Download Manager with progress dialog

查看:78
本文介绍了带有进度对话框的 Android 下载管理器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 android 下载管理器编写了一个 android 应用程序,并尝试使用以下代码显示下载进度.

I have coded an android app using android download manager, and I try to show downloading progress using below code.

myTimer.schedule(new TimerTask() {

        public void run() {
            try {
                DownloadManager.Query q;
                q = new DownloadManager.Query();
                q.setFilterById(preferenceManager.getLong(strPref_Download_ID, 0));
                cursorTimer = downloadManager.query(q);
                cursorTimer.moveToFirst();
                int bytes_downloaded = cursorTimer.getInt(cursorTimer.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                bytes_total = cursorTimer.getInt(cursorTimer.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
                final int dl_progress = (int) ((double) bytes_downloaded * 100f / (double) bytes_total);
                mProgressDialog.setProgress((int) dl_progress);
            } catch (Exception e) {
            } finally {
            }
        }
    }, 0, 10);

一切正常,但进度对话框没有显示平滑的进度,这意味着我希望显示 1,2,3,4,5,6,.....100.

Everthing is working fine, but progress dialog is not showing smooth pregress that means I wish to show 1,2,3,4,5,6,.....100.

它最初显示为 0,然后突然变为 12%,然后变为 31% 等 100%.我的文件总大小为 26246026 字节,在 0% 时我下载的文件大小为 6668 字节,在 12% 时,我下载的文件大小为 3197660 字节,等等...

It's show initially 0, and suddenly change to 12% then 31% etc 100%. My file Total size is 26246026 bytes, at the time of 0% my downloaded file size is 6668 bytes, at the time of 12% my downloaded file size is 3197660 bytes, and etc...

推荐答案

来自文档,

public void schedule(TimerTask任务,长延时,长周期)

为重复固定延迟执行安排任务在特定延迟之后.

public void schedule (TimerTask task, long delay, long period)

Schedule a task for repeated fixed-delay execution after a specific delay.

参数
任务 - 要安排的任务.
延迟 - 第一次执行前的时间量(以毫秒为单位).
period - 后续执行之间的时间(以毫秒为单位).

Parameters
task - the task to schedule.
delay - amount of time in milliseconds before first execution.
period - amount of time in milliseconds between subsequent executions.

在这里,您的代码中有 10 毫秒的周期.这可能是问题所在.改为尝试 1 毫秒.

Here, you have a period of 10 millis in your code. That might be the problem. Try 1 millis instead.

myTimer.schedule(new TimerTask() {
}, 0, 1);

这篇关于带有进度对话框的 Android 下载管理器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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