【问题标题】:Downloading multiple files one by one with downloadmanager on Android using one Notification使用一个通知在 Android 上使用下载管理器一一下载多个文件
【发布时间】:2018-09-15 00:31:49
【问题描述】:

我想在 Android 上使用DownloadManager 下载多个文件。我希望在通知区域中只看到 1 个带有 ProgressBar 的通知,它将向我显示整个下载进度状态。有可能吗?

例如有 2 个文件,第一个是 1MB,第二个是 3Mb。当DownloadManager下载1MB时,进度状态为25%。

【问题讨论】:

  • 下载前通常不知道文件的大小。
  • 下载前我知道文件的大小。我有这个信息。在这种情况下我该怎么办?
  • 那么应该是可能的。你到底有什么问题?
  • 我只需要一个带有一个进度条的通知,我想使用下载管理器。
  • 是的,我们已经知道了。你只是在重复自己。请说出您遇到的具体问题。

标签: android download notifications progress-bar android-download-manager


【解决方案1】:

默认情况下,您所描述的行为已在 DownloadManager 中提供。您只需要排队下载几次,Android 操作系统会自动向您显示所有排队下载的进度。

要将多个下载排入队列,您必须执行类似的操作:

val url = "http://your-file1.extension"
val request = DownloadManager.Request(Uri.parse(url))
request.allowScanningByMediaScanner()
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
request.setDestinationInExternalPublicDir(WALKAHOLIC_SDCARD_DIRECTORY, "your-file1.extension")

val url2 = "http://your-file2.extension"
val request2 = DownloadManager.Request(Uri.parse(url2))
request2.allowScanningByMediaScanner()
request2.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)
request2.setDestinationInExternalPublicDir(YOUR_DESIRED_DOWNLOAD_DIRECTORY, "your-file2.extension")

val manager = context.getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val downloadId1 = manager.enqueue(request)
val downloadId2 = manager.enqueue(request2)

如您所见,我创建了 2 个请求(每个下载一个),并配置了通知可见性:

setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE)

除此之外,两个请求都已排队到DownloadManager

可能是在以前的 Android 版本中,下载内容是单独显示的。但至少在 Android 7、8 和 9 中以组合的方式向我展示。这样您就可以看到所有已请求下载的综合进度。

【讨论】:

    猜你喜欢
    • 2014-10-20
    • 1970-01-01
    • 1970-01-01
    • 2016-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-10
    • 1970-01-01
    相关资源
    最近更新 更多