【问题标题】:Progress Bar of DownloadManager with percentage带百分比的 DownloadManager 进度条
【发布时间】:2018-07-31 16:21:38
【问题描述】:

我想得到这样的一些:

但我不知道如何获得百分比。

我读过这篇文章:

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

【问题讨论】:

    标签: android android-download-manager


    【解决方案1】:

    开始下载时创建通知

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

    【讨论】:

    • 但是,如果下载管理器出现错误并且下载失败,¿如何删除通知? =( 不只有一个通知。同时有多个通知。
    • 您应该跟踪您的通知 ID,然后您可以调用 cancel(id)。 id 是在你调用 notify(id,notification) 时设置的。只需保存该 ID,您可以稍后取消它。如果您设置 notification.flags = Notification.FLAG_AUTO_CANCEL; 您也可以在单击它时将其关闭....无论如何这是题外话..你应该问一个关于取消通知的不同问题。希望对您有所帮助!
    • 感谢您的帮助! =)
    猜你喜欢
    • 2017-08-03
    • 1970-01-01
    • 1970-01-01
    • 2012-12-23
    • 1970-01-01
    • 2014-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多