Cuelogic Blog Icon
Cuelogic Career Icon
Home > Blog > All > Using Download Manager in Android OS.

Using Download Manager in Android OS.

We come across many scenarios in android app development where we need to download some files or performs long running download tasks in the background. To achieve this the android OS provides a useful service call the

Download Manager.

Introduced in Android 2.3. (API 9)

It is a system service which allows to handle long-running HTTP downloads in the background and notify the triggering application via a broadcast receiver once the download is finished.

The advantage of using this service is that it takes care of HTTP interactions and retries downloads after failures or across connectivity changes and system reboots.

The following code snippet will help you understand how to create an instance of the Download Manager, with the desired URL.

String Url = "Your download URL";
 downloadFromDlMangr(Url);
 
public String downloadFromDlMangr(String DownloadUrl) 
{
        String result;

        dMgr = (DownloadManager) context
                .getSystemService(context.DOWNLOAD_SERVICE);
        DownloadManager.Request dmReq = new DownloadManager.Request(
                Uri.parse(DownloadUrl));
        dmReq.setTitle("Downloading");
        dmReq.setDescription("Downloading XYZ file");
        Uri ebookUri = Uri.fromFile(dir);
        dmReq.setDestinationUri(ebookUri);

        DownloaderReceiver.downloadId = dMgr.enqueue(dmReq);
        result = "success";

        return result;
 }

Here Uri ebookUri = Uri.fromFile(dir); and dmReq.setDestinationUri(ebookUri); specify where you want your downloaded files to be stored.

The above image shows you how the download manager works. You get the Downloading message with the progress in the notification bar.

Updating progress bar with the percentage wise download of your download manager.

In scenarios where you need to update or show the progress of your download so far, you can follow the below code example to get integer wise percentage.

// Get the downloaded percent
    private int getProgressPercentage() {

        int DOWNLOADED_BYTES_SO_FAR_INT = 0, TOTAL_BYTES_INT = 0, PERCENTAGE = 0;
        DownloadManager dMgr = null;

        try {
            dMgr = ServiceConnection.dMgr;
            Cursor c = dMgr.query(new DownloadManager.Query()
                    .setFilterById(DownloaderReceiver.downloadId));

            if (c.moveToFirst()) {
                DOWNLOADED_BYTES_SO_FAR_INT = (int) c
                        .getLong(c
                                .getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
                TOTAL_BYTES_INT = (int) c
                        .getLong(c
                                .getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
            }

            System.out.println("PERCEN ------" + DOWNLOADED_BYTES_SO_FAR_INT
                    + " ------ " + TOTAL_BYTES_INT + "****" + PERCENTAGE);
            PERCENTAGE = (DOWNLOADED_BYTES_SO_FAR_INT * 100 / TOTAL_BYTES_INT);
            System.out.println("PERCENTAGE % " + PERCENTAGE);

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

        return PERCENTAGE;
    }

Since this service was introduced in Android 2.3. (API 9), the drawback is it cannot be used for lower level apis.