Android download manager with progress dialog

I have coded an Android app using the Android Download Manager and I am trying to show the progress using the code below.

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 works fine, but the progress dialog doesn't show a smooth preview, which means I want to show 1,2,3,4,5,6, ..... 100.

It shows 0 at first and suddenly changes to 12%, then 31%, etc. one hundred%. My file Total size is 26246026 bytes, at the time of 0% my uploaded file size is 6668 bytes, at the time of 12% my uploaded file size is 3197660 bytes, etc.

+3


source to share


2 answers


From the documentation,

public scheduling void (TimerTask, long delay, long period)

 Schedule a task to run again with a fixed delay after a specified delay.

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



Here you have a period of 10 millis in your code. This could be a problem. Try 1 millis instead.

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

      

0


source


First of all, don't ask too often, you can hang your interface and use ValueAnimator

for a smooth transition.



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

    }

      

0


source







All Articles