4

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

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.

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...

Nizam
  • 5,698
  • 9
  • 45
  • 57
user3068659
  • 443
  • 5
  • 14

2 Answers2

0

From documentation,

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

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

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

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);
Community
  • 1
  • 1
Nizam
  • 5,698
  • 9
  • 45
  • 57
0

First of all don't query too frequent it may hang your UI and use ValueAnimator for changing progress smoothly.

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

    }
Karan Kalsi
  • 789
  • 3
  • 21