Code example for DownloadManager

Methods: queryremove

0
    private void checkDownloadStatus() { 
        DownloadManager.Query query = new DownloadManager.Query();
        long id = preferenceManager.getLong(PREFERENCE_RELEASE_ID, 0);
        query.setFilterById(id);
 
        Cursor cursor = downloadManager.query(query);
        if (cursor.moveToFirst()) {
            int columnIndex = cursor.getColumnIndex(COLUMN_STATUS);
            int status = cursor.getInt(columnIndex);
            int columnReason = cursor.getColumnIndex(COLUMN_REASON);
            int reason = cursor.getInt(columnReason);
 
            switch (status) {
                case STATUS_FAILED:
                    String failedReason = "";
 
                    switch (reason) {
                        case ERROR_CANNOT_RESUME:
                            failedReason = "ERROR_CANNOT_RESUME";
                            break; 
 
                        case ERROR_DEVICE_NOT_FOUND:
                            failedReason = "ERROR_DEVICE_NOT_FOUND";
                            break; 
 
                        case ERROR_FILE_ALREADY_EXISTS:
                            failedReason = "ERROR_FILE_ALREADY_EXISTS";
                            break; 
 
                        case ERROR_FILE_ERROR:
                            failedReason = "ERROR_FILE_ERROR";
                            break; 
 
                        case ERROR_HTTP_DATA_ERROR:
                            failedReason = "ERROR_HTTP_DATA_ERROR";
                            break; 
 
                        case ERROR_INSUFFICIENT_SPACE:
                            failedReason = "ERROR_INSUFFICIENT_SPACE";
                            break; 
 
                        case ERROR_TOO_MANY_REDIRECTS:
                            failedReason = "ERROR_TOO_MANY_REDIRECTS";
                            break; 
 
                        case ERROR_UNHANDLED_HTTP_CODE:
                            failedReason = "ERROR_UNHANDLED_HTTP_CODE";
                            break; 
 
                        case ERROR_UNKNOWN:
                            failedReason = "ERROR_UNKNOWN";
                            break; 
                    } 
 
                    displayStatus("FAILED", failedReason);
                    break; 
 
                case STATUS_PAUSED:
                    String pausedReason = "";
 
                    switch (reason) {
                        case PAUSED_QUEUED_FOR_WIFI:
                            pausedReason = "PAUSED_QUEUED_FOR_WIFI";
                            break; 
 
                        case PAUSED_UNKNOWN:
                            pausedReason = "PAUSED_UNKNOWN";
                            break; 
 
                        case PAUSED_WAITING_FOR_NETWORK:
                            pausedReason = "PAUSED_WAITING_FOR_NETWORK";
                            break; 
 
                        case PAUSED_WAITING_TO_RETRY:
                            pausedReason = "PAUSED_WAITING_FOR_RETRY";
                            break; 
                    } 
 
                    displayStatus("Paused", pausedReason);
                    break; 
 
                case STATUS_PENDING:
                    break; 
 
                case STATUS_RUNNING:
                    break; 
 
                case STATUS_SUCCESSFUL:
 
                    if (!isFileExists(getSumFile())) { 
                        requestDownload(format("%s.sha256sum", release));
                    } 
                    else { 
                        startFlashActivity(); 
                    } 
                    break; 
            } 
        } 
    } 
 
    private void displayStatus(String status, String reason) {
 
        final TextView tvFlashCompleted = (TextView) findViewById(R.id.textViewFlashCompleted);
 
        if (reason != null) {
            tvFlashCompleted.setText(format("%s: %s", status, reason));
        } 
 
        else { 
            tvFlashCompleted.setText(format("%s", status));
        } 
    } 
 
    private void requestDownload(String filename) {
 
        DownloadManager.Request request = new DownloadManager.Request(parse(format("%s/%s",
                targetUrl, filename)));
        request.setTitle(format("%s %s", getString(R.string.app_name), "Download"));
        request.setDescription(filename);
        request.allowScanningByMediaScanner();
        request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
        request.setDestinationInExternalPublicDir(DIRECTORY_DOWNLOADS, filename);
 
        long id = downloadManager.enqueue(request);
 
        Editor editor = preferenceManager.edit();
        editor.putLong(PREFERENCE_RELEASE_ID, id);
        editor.commit();
    } 
 
    private void startFlashActivity() { 
        if (isMatchedSum(getFile())) { 
            downloadManager.remove(preferenceManager.getLong(PREFERENCE_RELEASE_ID, 0));
            Intent intent = new Intent(this, FlashActivity.class);
            startActivity(intent);
        } else { 
            displayStatus("Failed", "Checksum does not match"); 
        }