Search code examples
androidandroid-download-manager

Progress Bar of DownloadManager with percentage


I would like to obtain like some this:

Percentage

But I don't know how I can to obtain the percentage.

I've read this posts:

Post 1

Post 2

Other web

But I haven't gotten any good result =(

¿Any idea?

I can get the total size and the bytes downloaded for a file, but I don't know how I can get that the progress bar shows these information in a percentage wa

int sizeIndex = c.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES);
int downloadedIndex = c.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR);
int size = c.getInt(sizeIndex);
int downloaded = c.getInt(downloadedIndex);
int percentage = (downloaded * 100 / size);

Solution

  • Just create a notification when you start downloading

        mNotifyManager = (NotificationManager) getApplicationContext().getSystemService(getApplicationContext().NOTIFICATION_SERVICE);
        mBuilder = new NotificationCompat.Builder(this);
        mBuilder.setContentTitle("Downloading file")
                .setContentText("Downloading... 0%")
                .setSmallIcon(R.drawable.your_icon);
        mNotifyManager.notify(YOUR_TAG, mBuilder.build());
    

    And then you can update progress calling that same notification whit YOUR_TAG

        mBuilder.setContentText("Downloading... " + CURRENT_BYTES * 100.0/TOTAL_BYTES + "%")
                .setProgress(TOTAL_BYTES, CURRENT_BYTES, false);
            mNotifyManager.notify(YOUR_TAG, mBuilder.build());