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

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

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();
pausedDownloadQuery.setFilterByStatus(DownloadManager.STATUS_PAUSED);
// 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 = 
 pausedDownloads.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES); 
int bytesDLIdx = 
 pausedDownloads.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
// 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(title).append(“\n”);
 sb.append(reasonString).append(“\n”);
 sb.append(“Downloaded “).append(bytesDL).append(“ / “ ).append(fileSize);
 // Display the status 
 Log.d(“DOWNLOAD”, sb.toString());
}
// Close the result Cursor.
pausedDownloads.close();

Leave a Comment