The Download Manager is a service for handling long-running downloads. The Download Manager handles the HTTP connection and check for connectivity changes, system reboots, etc. to make sure that the download completes successfully. The Download Manager will notify the Activity using a broadcast receiver once the download is complete. You can also specify the connectivity conditions under which to execute the download. If the allowed network type is not available you will receive the error saying – “Aborting request for download: download was requested to not use the current network type“. For example to restrict the download of a large file only when connected to WiFi you can specify the following

1
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI);

Please Note: The Download Manager activity needs permission for internet and if you are saving your data in External Storage. Here is the snippet that you need to add to your android manifest file 

<uses-permissionandroid:name="android.permission.INTERNET"/>
<uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
For start download of any button, click implement the following lines.
DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
Uri Download_Uri = Uri.parse("https://cloudup.com/files/inYVmLryD4p/download");
DownloadManager.Request request = new DownloadManager.Request(Download_Uri);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
request.setAllowedOverRoaming(false);
request.setTitle("My Data Download");
request.setDescription("Download using DownloadManager.");
request.setDestinationInExternalFilesDir(this, Environment.DIRECTORY_DOWNLOADS, Download_Uri.getLastPathSegment() + ".mp3");
downloadReference = downloadManager.enqueue(request);
You can get download status using cursor query
DownloadManager.Query myDownloadQuery = new DownloadManager.Query();
myDownloadQuery.setFilterById(downloadReference);
Cursor cursor = downloadManager.query(myDownloadQuery);
if (cursor.moveToFirst()) {
    checkStatus(cursor);
}
private void checkStatus(Cursor cursor) {

    int columnIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
    int status = cursor.getInt(columnIndex);
    int columnReason = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
    int reason = cursor.getInt(columnReason);
    int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
    String filename = cursor.getString(filenameIndex);

    String statusText = "";
    String reasonText = "";

    switch (status) {
        case DownloadManager.STATUS_FAILED:
            statusText = "STATUS_FAILED";
            switch (reason) {
                case DownloadManager.ERROR_CANNOT_RESUME:
                    reasonText = "ERROR_CANNOT_RESUME";
                    break;
                case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                    reasonText = "ERROR_DEVICE_NOT_FOUND";
                    break;
                case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                    reasonText = "ERROR_FILE_ALREADY_EXISTS";
                    break;
                case DownloadManager.ERROR_FILE_ERROR:
                    reasonText = "ERROR_FILE_ERROR";
                    break;
                case DownloadManager.ERROR_HTTP_DATA_ERROR:
                    reasonText = "ERROR_HTTP_DATA_ERROR";
                    break;
                case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                    reasonText = "ERROR_INSUFFICIENT_SPACE";
                    break;
                case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                    reasonText = "ERROR_TOO_MANY_REDIRECTS";
                    break;
                case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                    reasonText = "ERROR_UNHANDLED_HTTP_CODE";
                    break;
                case DownloadManager.ERROR_UNKNOWN:
                    reasonText = "ERROR_UNKNOWN";
                    break;
            }
            break;
        case DownloadManager.STATUS_PAUSED:
            statusText = "STATUS_PAUSED";
            switch (reason) {
                case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
                    reasonText = "PAUSED_QUEUED_FOR_WIFI";
                    break;
                case DownloadManager.PAUSED_UNKNOWN:
                    reasonText = "PAUSED_UNKNOWN";
                    break;
                case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
                    reasonText = "PAUSED_WAITING_FOR_NETWORK";
                    break;
                case DownloadManager.PAUSED_WAITING_TO_RETRY:
                    reasonText = "PAUSED_WAITING_TO_RETRY";
                    break;
            }
            break;
        case DownloadManager.STATUS_PENDING:
            statusText = "STATUS_PENDING";
            break;
        case DownloadManager.STATUS_RUNNING:
            statusText = "STATUS_RUNNING";
            break;
        case DownloadManager.STATUS_SUCCESSFUL:
            statusText = "STATUS_SUCCESSFUL";
            reasonText = "Filename:\n" + filename;
            break;
    }


    Toast toast = Toast.makeText(MainActivity.this,
            statusText + "\n" +
                    reasonText,
            Toast.LENGTH_LONG);
    toast.setGravity(Gravity.TOP, 25, 400);
    toast.show();

}


The Download Manager will notify the Activity using a broadcast receiver once the download is complete. 


IntentFilter filter = new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
registerReceiver(downloadReceiver, filter);

private BroadcastReceiver downloadReceiver = new BroadcastReceiver() {

    @Override
    public void onReceive(Context context, Intent intent) {

        try {
            //check if the broadcast message is for our Enqueued download
            long referenceId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
            String action = intent.getAction();
            if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(action)) {
                Intent i = new Intent();
                i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                i.setAction(DownloadManager.ACTION_VIEW_DOWNLOADS);
                context.startActivity(i);
            }

            if (action.equals(DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
                if (progress.isShowing()) {
                    progress.dismiss();
                }
               

            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
};

https://github.com/android-inheritx/DownloadMangerJetpack

You may also like

Leave a Reply