@Override publicvoidrun() { while ((soFar < total || total <= 0) && (status == DownloadManager.STATUS_RUNNING || status == DownloadManager.STATUS_PENDING || status == DownloadManager.STATUS_PAUSED || status == 0)) { query(); }
}
privatevoidquery() { DownloadManager.Queryquery=newDownloadManager.Query(); query.setFilterById(downloadId); Cursorcursor= downloadManager.query(query); if (cursor.moveToNext()) { status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
soFar = cursor.getLong( cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR)); if (total <= 0) { total = cursor.getLong( cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES)); }
progress = (int) ((soFar * 100f) / total);
if (status == DownloadManager.STATUS_PENDING) { if (callback != null) callback.callback(0, soFar, total, progress); } elseif (status == DownloadManager.STATUS_SUCCESSFUL || status == DownloadManager.STATUS_FAILED) { if (callback != null) callback.callback( status == DownloadManager.STATUS_SUCCESSFUL ? 2 : 3, soFar, total, progress); } else { if (callback != null) callback.callback(0, soFar, total, progress); }