【问题标题】:Android Download Manager with progress dialog带有进度对话框的 Android 下载管理器
【发布时间】:2017-08-12 14:09:53
【问题描述】:

我使用 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);

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

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

【问题讨论】:

    标签: android progressdialog android-download-manager


    【解决方案1】:

    来自文档,

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

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

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

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

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

    【讨论】:

      【解决方案2】:

      首先不要查询太频繁,它可能会挂起你的 UI,使用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();
      
          }
      

      【讨论】:

      • 请您分享您现在如何实现的代码
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-18
      • 2011-07-27
      • 1970-01-01
      • 2022-01-02
      • 1970-01-01
      相关资源
      最近更新 更多