Download Manager Query in Android

The query method takes a DownloadManager.Query object as a parameter. Use the setFilterById method on a Query object to specify a sequence of download reference IDs, or use the setFilterByStatus method to filter on a download status using one of the DownloadManager .STATUS_* constants to specify running, paused, failed, or successful downloads

The Download Manager includes a number of COLUMN_* static String constants that you can use to query the result Cursor. You can fi nd details for each download, including the status, fi les size, bytes downloaded so far, title, description, URI, local filename and URI, media type, and Media Provider download URI

Finding details of completed downloads

public void onReceive(Context context, Intent intent) {
 long reference = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
 if (reference == myDownloadReference) {
 Query myDownloadQuery = new Query();
 Cursor myDownload = downloadManager.query(myDownloadQuery);
 if (myDownload.moveToFirst()) {
 int fileNameIdx = 
 int fileUriIdx = 
 String fileName = myDownload.getString(fileNameIdx);
 String fileUri = myDownload.getString(fileUriIdx);
 // TODO Do something with the file.

For downloads that are either paused or have failed, you can query the COLUMN_REASON column to fi nd the cause represented as an integer.

In the case of STATUS_PAUSED downloads, you can interpret the reason code by using one of the DownloadManager.PAUSED_* static constants to determine if the download has been paused while waiting for network connectivity, a Wi-Fi connection, or pending a retry

For STATUS_FAILED downloads, you can determine the cause of failure using the DownloadManager.ERROR_* codes. Possible error codes include lack of a storage device, insufficient free space, duplicate filenames, or HTTP errors.

Finding details of paused downloads

// Obtain the Download Manager Service.
String serviceString = Context.DOWNLOAD_SERVICE;
DownloadManager downloadManager;
downloadManager = (DownloadManager)getSystemService(serviceString);
// Create a query for paused downloads.
Query pausedDownloadQuery = new Query();
// Query the Download Manager for paused downloads.
Cursor pausedDownloads = downloadManager.query(pausedDownloadQuery);
// Find the column indexes for the data we require.
int reasonIdx = pausedDownloads.getColumnIndex(DownloadManager.COLUMN_REASON);
int titleIdx = pausedDownloads.getColumnIndex(DownloadManager.COLUMN_TITLE);
int fileSizeIdx = 
int bytesDLIdx = 
// Iterate over the result Cursor.
while (pausedDownloads.moveToNext()) {
 // Extract the data we require from the Cursor.
 String title = pausedDownloads.getString(titleIdx);
 int fileSize = pausedDownloads.getInt(fileSizeIdx);
 int bytesDL = pausedDownloads.getInt(bytesDLIdx);
 // Translate the pause reason to friendly text.
 int reason = pausedDownloads.getInt(reasonIdx);
 String reasonString = “Unknown”;
 switch (reason) {
 case DownloadManager.PAUSED_QUEUED_FOR_WIFI : 
 reasonString = “Waiting for WiFi”; break;
 case DownloadManager.PAUSED_WAITING_FOR_NETWORK : 
 reasonString = “Waiting for connectivity”; break;
 case DownloadManager.PAUSED_WAITING_TO_RETRY :
reasonString = “Waiting to retry”; break;
 default : break;
 // Construct a status summary
 StringBuilder sb = new StringBuilder();
 sb.append(“Downloaded “).append(bytesDL).append(“ / “ ).append(fileSize);
 // Display the status 
 Log.d(“DOWNLOAD”, sb.toString());
// Close the result Cursor.

Leave a Comment