2

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);
adlagar
  • 877
  • 10
  • 31

1 Answers1

3

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());
Benjamin Fell
  • 393
  • 2
  • 11
  • But, if some error occurs with Download Manager and the download fails, ¿how can I delete the notification? =( There aren't only one notification. There are multiple notifications at the same time. – adlagar Sep 16 '15 at 11:29
  • You should keep track of your notification id's and then you can just call cancel(id). The id is set when you call notify(id,notification). Just save that id and you can cancel it later on. You can also make it dismissable when you click it if you set notification.flags = Notification.FLAG_AUTO_CANCEL; ….Anyways this is off-topic.. you should ask a different question about cancelling notifications. Hope it helped! – Benjamin Fell Sep 16 '15 at 13:41
  • 1
    Thanks for your help! =) – adlagar Sep 16 '15 at 13:42