1

I'm trying to show the download progress of DownloadManager in percentage as shown in the notification bar. I've read up some answer here. However, the method provided is using the Thread. As my understanding,Thread is not a good way.

Is there any alternative way to do the same thing? Or using Thread is just fine in this case?

Edit 1: Added in the code in my project.

new Thread(new Runnable() {

        @Override
        public void run() {

            boolean downloading = true;

            while (downloading) {

                DownloadManager.Query q = new DownloadManager.Query();
                q.setFilterById(downloadId);

                Cursor cursor = downloadManager.query(q);
                cursor.moveToFirst();
                int bytes_downloaded = cursor.getInt(cursor
                        .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                int bytes_total = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));

                if (cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS)) == DownloadManager.STATUS_SUCCESSFUL) {
                    downloading = false;
                }

                final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total);

                ((Activity) mContext).runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        holder.mTextView.setText(String.valueOf(dl_progress) + "%");
                    }
                });
                cursor.close();
            }
        }
    }).start();
Community
  • 1
  • 1
wenyang9319
  • 180
  • 2
  • 13
  • Welcome to StackOverflow. Please note, that this is not a free code-writing service, but we are eager to help fellow programmers (and aspirants) writing their own code. Please read the help topics on [How To Ask a Good Question](http://stackoverflow.com/help/how-to-ask). Afterwards, please update your question with the code you have written thus far in order to complete the task(s) you wish to achieve. – Quintin Balsdon Nov 11 '16 at 08:53
  • Please have a looks at this: http://www.101apps.co.za/articles/using-the-downloadmanager-to-manage-your-downloads.html – Quintin Balsdon Nov 11 '16 at 08:54
  • @QuintinBalsdon thanks for the advice and suggestion, I updated my question. Unfortunately, I cant find answer from the website =(. What I needed is the percentage of the download progress or the file size that not yet downloaded – wenyang9319 Nov 11 '16 at 09:07
  • You should definitely not use a Thread. There are several mechanisms for creating asynchronous communication like an AsyncTask or an IntentService. I would recommend use an AsyncTask for now http://stackoverflow.com/questions/3028306/download-a-file-with-android-and-showing-the-progress-in-a-progressdialog – Quintin Balsdon Nov 11 '16 at 09:13
  • Alright, thanks for your answer. May I know what is the main reason we should avoid using Thread? – wenyang9319 Nov 11 '16 at 09:21
  • Threads can block the UI and cause exceptions if you try to modify the UI from inside the thread, or any subsequent method invoked when exiting the thread. – Quintin Balsdon Nov 11 '16 at 09:26
  • Alright, thank you for all your assistance =) @QuintinBalsdon – wenyang9319 Nov 11 '16 at 09:31

0 Answers0