简体   繁体   中英

Progress Bar of DownloadManager with percentage

I would like to obtain like some this:

百分比

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);

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM