DownloadManager 进度条百分比

Progress Bar of DownloadManager with percentage

我想得到这样的一些:

但是我不知道怎么才能得到这个百分比。

我读过这篇文章:

Post 1

Post 2

Other web

但是我没有得到什么好的结果=(

¿有什么想法吗?

我可以获得文件的总大小和下载的字节数,但我不知道如何让进度条以百分比显示这些信息 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);

只需在开始下载时创建通知

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

然后您可以调用相同的通知更新进度 YOUR_TAG

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