Last active
December 14, 2023 17:39
-
-
Save tolmachevroman/2c3358faad90aa802dfb to your computer and use it in GitHub Desktop.
Reflect DownloadManager progress of downloading multiple files in ProgressBar widget
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Fetches how many bytes have been downloaded so far and updates ProgressBar | |
*/ | |
class DownloadProgressCounter extends Thread { | |
private final long downloadId; | |
private final DownloadManager.Query query; | |
private Cursor cursor; | |
private int lastBytesDownloadedSoFar; | |
private int totalBytes; | |
public DownloadProgressCounter(long downloadId) { | |
this.downloadId = downloadId; | |
this.query = new DownloadManager.Query(); | |
query.setFilterById(this.downloadId); | |
} | |
@Override | |
public void run() { | |
while (downloadId > 0) { | |
try { | |
Thread.sleep(300); | |
cursor = manager.query(query); | |
if (cursor.moveToFirst()) { | |
//get total bytes of the file | |
if (totalBytes <= 0) { | |
totalBytes = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); | |
} | |
final int bytesDownloadedSoFar = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); | |
if (bytesDownloadedSoFar == totalBytes && totalBytes > 0) { | |
this.interrupt(); | |
} else { | |
//update progress bar | |
runOnUiThread(new Runnable() { | |
@Override | |
public void run() { | |
mProgressBar.setProgress(mProgressBar.getProgress() + (bytesDownloadedSoFar - lastBytesDownloadedSoFar)); | |
lastBytesDownloadedSoFar = bytesDownloadedSoFar; | |
} | |
}); | |
} | |
} | |
cursor.close(); | |
} catch (Exception e) { | |
return; | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class LessonDetailsActivity extends Activity { | |
private ProgressBar mProgressBar; | |
/** | |
* Some code here... | |
* */ | |
//download and save file in SD | |
private void download(String url) { | |
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); | |
if (Build.VERSION.SDK_INT >= 11) { | |
request.allowScanningByMediaScanner(); | |
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_HIDDEN); | |
} | |
//expected url ends like /uploads/content/15/image/23_FAJE457F4A5NQBJ.MEDIUM.jpg | |
String[] paths = url.split("/"); | |
String fileName = paths[paths.length - 1]; | |
request.setDestinationInExternalPublicDir(Utils.getLessonFolderPath(mBundle.getString(Lesson._ID)), fileName); | |
registerReceiver(mDownloadReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); | |
mIsReceiverRegistered = true; | |
// get download service and enqueue file | |
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); | |
new DownloadProgressCounter(manager.enqueue(request)).start(); | |
} | |
} |
Can you double-check if this works for multiple downloads?
Can you double-check if this works for multiple downloads?
@lune856 have you completed multiple downloads from download manager ?
great !!
@abiemann manager is variable of DownloadManager manager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE);
Thanks!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
this is broken;
manager
is left undefined in DownloadProgressCounter