I am looking for a better way of doing this also, but so far I am planning to just poll for progress every 1sec or so.
DownloadManager mgr = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
long id = mgr.enqueue(request);
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(id);
Cursor cursor = mgr.query(q);
cursor.moveToFirst();
int bytes_downloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
cursor.close();
Edit:
A FileObserver can help with this. This is the skeleton of one I have put together to help keep track of which files our app has downloaded. Start it in an activity or service's onStart and stop it in onStop. Combined with a manual synchronization of the state of things during onStart, this can give you a pretty complete picture of what's going on.
For progress in particular, watching for the OPEN/CLOSE_WRITE events can help you decide when to start/stop polling the DownloadManager for updates.
public class DownloadsObserver extends FileObserver {
public static final String LOG_TAG = DownloadsObserver.class.getSimpleName();
private static final int flags =
FileObserver.CLOSE_WRITE
| FileObserver.OPEN
| FileObserver.MODIFY
| FileObserver.DELETE
| FileObserver.MOVED_FROM;
// Received three of these after the delete event while deleting a video through a separate file manager app:
// 01-16 15:52:27.627: D/APP(4316): DownloadsObserver: onEvent(1073741856, null)
public DownloadsObserver(String path) {
super(path, flags);
}
@Override
public void onEvent(int event, String path) {
Log.d(LOG_TAG, "onEvent(" + event + ", " + path + ")");
answered 2011-11-03T21:41:21.027