Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/214.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带有进度对话框的Android下载管理器_Android_Progressdialog_Android Download Manager - Fatal编程技术网

带有进度对话框的Android下载管理器

带有进度对话框的Android下载管理器,android,progressdialog,android-download-manager,Android,Progressdialog,Android Download Manager,我已经使用android下载管理器编写了一个android应用程序,并尝试使用以下代码显示下载进度 myTimer.schedule(new TimerTask() { public void run() { try { DownloadManager.Query q; q = new DownloadManager.Query(); q.setFilterB

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

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

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

来自文档

公共无效计划(TimerTask任务、长延迟、长周期)

为重复的固定延迟执行计划任务 在特定的延迟之后

参数
任务-要计划的任务
delay—首次执行前的时间量(毫秒)
period—后续执行之间的时间量(以毫秒为单位)

在这里,代码中有一个10毫秒的周期。这可能就是问题所在。试试1毫升

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

首先,不要过于频繁地查询,这可能会挂起您的用户界面,并使用
ValueAnimator
来平稳地更改进度

   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);
               changeProgressSmoothly((int) dl_progress);
            } catch (Exception e) {
            } finally {
            }
        }
    }, 0, 5000);



private void changeProgressSmoothly(int progress) {
        ValueAnimator va = ValueAnimator.ofInt(mProgressDialog.getProgress(), progress);

        int mDuration = 2000; //in millis
        va.setDuration(mDuration);
        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            public void onAnimationUpdate(ValueAnimator animation) {
                mProgressDialog.setProgress((int) animation.getAnimatedValue());
            }
        });
        va.start();

    }

请分享您的代码,说明您现在是如何实现它的