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";
public String downloadFromDlMangr(String DownloadUrl) 
        String result;

        dMgr = (DownloadManager) context
        DownloadManager.Request dmReq = new DownloadManager.Request(
        dmReq.setDescription("Downloading XYZ file");
        Uri ebookUri = Uri.fromFile(dir);

        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.

Download manager

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() {

        DownloadManager dMgr = null;

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

            if (c.moveToFirst()) {
                DOWNLOADED_BYTES_SO_FAR_INT = (int) c
                TOTAL_BYTES_INT = (int) c

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

        } catch (Exception e) {

        return PERCENTAGE;

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