简体   繁体   English

具有百分比的DownloadManager进度栏

[英]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 发布1

Post 2 发布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 然后,您可以通过调用相同的通知YOUR_TAG来更新进度

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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