Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to request multiple downloads using Download Manager in Android 2.3.3

How to request Android download manager to download multiple files at the same time. Also I would like to know each and every file download status.

like image 539
Ganesh K Avatar asked Jan 23 '12 11:01

Ganesh K


Video Answer


2 Answers

1. Register listener for download complete
  IntentFilter intentFilter = new IntentFilter(
            DownloadManager.ACTION_DOWNLOAD_COMPLETE);
    registerReceiver(downloadReceiver, intentFilter);
2.Make request

Uri downloadUri = Uri.parse(entry.getValue());
            DownloadManager.Request request = new DownloadManager.Request(
                    downloadUri);

            request.setDestinationUri(path_to _file_store)));
            downloadManager.enqueue(request);
3.Check status in listener
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context arg0, Intent intent) {
        String action = intent.getAction();
        if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
            long downloadId = intent.getLongExtra(
                    DownloadManager.EXTRA_DOWNLOAD_ID, 0);
            System.out.println("download id=" + downloadId);
            CheckDwnloadStatus(downloadId);
        }
    }
};
private void CheckDwnloadStatus(long id) {

    // TODO Auto-generated method stub
    DownloadManager.Query query = new DownloadManager.Query();

    query.setFilterById(id);
    Cursor cursor = downloadManager.query(query);
    if (cursor.moveToFirst()) {
        int columnIndex = cursor
                .getColumnIndex(DownloadManager.COLUMN_STATUS);
        int status = cursor.getInt(columnIndex);
        int columnReason = cursor
                .getColumnIndex(DownloadManager.COLUMN_REASON);
        int reason = cursor.getInt(columnReason);

        switch (status) {
        case DownloadManager.STATUS_FAILED:
            String failedReason = "";
            switch (reason) {
            case DownloadManager.ERROR_CANNOT_RESUME:
                failedReason = "ERROR_CANNOT_RESUME";
                break;
            case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                failedReason = "ERROR_DEVICE_NOT_FOUND";
                break;
            case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                failedReason = "ERROR_FILE_ALREADY_EXISTS";
                break;
            case DownloadManager.ERROR_FILE_ERROR:
                failedReason = "ERROR_FILE_ERROR";
                break;
            case DownloadManager.ERROR_HTTP_DATA_ERROR:
                failedReason = "ERROR_HTTP_DATA_ERROR";
                break;
            case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                failedReason = "ERROR_INSUFFICIENT_SPACE";
                break;
            case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                failedReason = "ERROR_TOO_MANY_REDIRECTS";
                break;
            case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                failedReason = "ERROR_UNHANDLED_HTTP_CODE";
                break;
            case DownloadManager.ERROR_UNKNOWN:
                failedReason = "ERROR_UNKNOWN";
                break;
            }

            Toast.makeText(this, "FAILED: " + failedReason,
                    Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PAUSED:
            String pausedReason = "";

            switch (reason) {
            case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                pausedReason = "PAUSED_QUEUED_FOR_WIFI";
                break;
            case DownloadManager.PAUSED_UNKNOWN:
                pausedReason = "PAUSED_UNKNOWN";
                break;
            case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                pausedReason = "PAUSED_WAITING_FOR_NETWORK";
                break;
            case DownloadManager.PAUSED_WAITING_TO_RETRY:
                pausedReason = "PAUSED_WAITING_TO_RETRY";
                break;
            }

            Toast.makeText(this, "PAUSED: " + pausedReason,
                    Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_PENDING:
            Toast.makeText(this, "PENDING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_RUNNING:
            Toast.makeText(this, "RUNNING", Toast.LENGTH_LONG).show();
            break;
        case DownloadManager.STATUS_SUCCESSFUL:
            caluclateLoadingData();
            // Toast.makeText(this, "SUCCESSFUL", Toast.LENGTH_LONG).show();
            // GetFile();
            break;
        }
    }
}
like image 138
Dayanand Waghmare Avatar answered Oct 10 '22 18:10

Dayanand Waghmare


Request the first one.

Then, request the second one.

Then, request the third one.

Continue as needed.

Whether they download "at the same time" is not your concern, nor do you have control over it. They will download when DownloadManager decides to download them, which may be simultaneously or not.

like image 20
CommonsWare Avatar answered Oct 10 '22 19:10

CommonsWare