Show download progress within an action using DownloadManager

I am trying to reproduce the same progress as DownloadManager in the notification panel inside my application, but my progress is never published. I am trying to update it with runOnUiThread (), but for some reason it has not been updated.

my download:

String urlDownload = "https://dl.dropbox.com/s/ex4clsfmiu142dy/test.zip?token_hash=AAGD-XcBL8C3flflkmxjbzdr7_2W_i6CZ_3rM5zQpUCYaw&dl=1"; DownloadManager.Request request = new DownloadManager.Request(Uri.parse(urlDownload)); request.setDescription("Testando"); request.setTitle("Download"); request.allowScanningByMediaScanner(); request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "teste.zip"); final DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); final long downloadId = manager.enqueue(request); final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressBar1); new Thread(new Runnable() { @Override public void run() { boolean downloading = true; while (downloading) { DownloadManager.Query q = new DownloadManager.Query(); q.setFilterById(downloadId); Cursor cursor = manager.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 double dl_progress = (bytes_downloaded / bytes_total) * 100; runOnUiThread(new Runnable() { @Override public void run() { mProgressBar.setProgress((int) dl_progress); } }); Log.d(Constants.MAIN_VIEW_ACTIVITY, statusMessage(cursor)); cursor.close(); } } }).start(); 

my statusMessage method:

 private String statusMessage(Cursor c) { String msg = "???"; switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) { case DownloadManager.STATUS_FAILED: msg = "Download failed!"; break; case DownloadManager.STATUS_PAUSED: msg = "Download paused!"; break; case DownloadManager.STATUS_PENDING: msg = "Download pending!"; break; case DownloadManager.STATUS_RUNNING: msg = "Download in progress!"; break; case DownloadManager.STATUS_SUCCESSFUL: msg = "Download complete!"; break; default: msg = "Download is nowhere in sight"; break; } return (msg); } 

My log is working fine, while my download is working, it says, "Downloading continues!" and when it is complete “Download full!” but the same thing does not happen in my progress, why? I really need help, other logics that are really appreciated.

+87
android progress-bar
Apr 03 '13 at 19:06 on
source share
3 answers

You divide two integers:

 final double dl_progress = (bytes_downloaded / bytes_total) * 100; 

Since bytes_downloaded less than bytes_total , (bytes_downloaded / bytes_total) will be 0, and your progress will always be 0.

Change your calculation to

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

to get progress in general (albeit half) percentiles.

+61
Apr 03 '13 at 21:45
source share

Paul answers correctly, but with large loads you will quickly reach the maximum of int and start to get a negative move. I used this to solve the problem:

 final int dl_progress = (int) ((bytes_downloaded * 100l) / bytes_total); 
+17
Nov 11 '13 at 20:27
source share

As Paul said, you divide two integers with the result always <1.

Always enter the number to , which calculates and returns at a floating point.

Remember to handle DivByZero.

 final int dl_progress = (int) ((double)bytes_downloaded / (double)bytes_total * 100f); 
+5
Jan 28 '14 at 9:19
source share



All Articles