无法使用 WorkManager 更新通知 ProgressBar 中的进度
Unable to update progress in ProgressBar of Notification using WorkManager
我想创建一个长运行 worker 来下载视频。根据我的理解,我们应该调用 createForegroundInfo()
来更新正在进行的通知。但是,我找不到有关如何更新通知中显示的确定进度条的信息。如下所示,我尝试在进度更新时调用 createForegroundInfo()
函数,但通知中的进度条没有更新。
注意: 正如您在下面的代码中看到的,我使用 setProgress(100, downloadProgress, false)
作为省略行根本没有在通知中显示进度条.
private fun createForegroundInfo(downloadProgress: Int, videoTitle: String): ForegroundInfo {
createNotificationChannel()
val cancelText = applicationContext.getString(R.string.all_text_cancel)
val cancelPendingIntent =
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id)
val notification = NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.app_download_channel_id)
)
.setContentTitle(videoTitle)
.setTicker(videoTitle)
.setProgress(100, downloadProgress, false)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setOngoing(true)
.addAction(R.drawable.ic_close, cancelText, cancelPendingIntent)
.build()
return ForegroundInfo(notificationId, notification)
}
我也试过更新进度,如下所示,但这只会更新使用 LiveData 在 Fragment 中观察到的进度,通知中的进度条不会更新。
val lastUpdate = workDataOf("Progress" to progress)
setProgress(lastUpdate)
同样如下所示,我观察到文档提到我们应该调用 setForegroundInfo()
而不是 createForegroundInfo()
来更新正在进行的通知。但是,没有名为 setForegroundInfo()
.
的方法
如前所述,我找不到更新正在进行的通知的进度条的方法。任何帮助将不胜感激。
文档中似乎有错误,方法名称是 setForeground()
而不是 setForegroundInfo()
。我能够使用下面显示的代码更新通知中的进度。
setForeground(createForegroundInfo(progress, videoTitle))
createForegroundInfo()
private fun createForegroundInfo(downloadProgress: Int, videoTitle: String): ForegroundInfo {
createDownloadNotificationChannel()
val cancelText = applicationContext.getString(R.string.all_text_cancel)
val cancelPendingIntent =
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id)
val notification = NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.app_download_channel_id)
)
.setContentTitle(videoTitle)
.setContentText("Downloading")
.setTicker(videoTitle)
.setProgress(100, downloadProgress, false)
.setSmallIcon(R.drawable.ic_stat_notification)
.setOngoing(true)
.addAction(R.drawable.ic_close, cancelText, cancelPendingIntent)
.build()
return ForegroundInfo(notificationId, notification)
}
我想创建一个长运行 worker 来下载视频。根据我的理解,我们应该调用 createForegroundInfo()
来更新正在进行的通知。但是,我找不到有关如何更新通知中显示的确定进度条的信息。如下所示,我尝试在进度更新时调用 createForegroundInfo()
函数,但通知中的进度条没有更新。
注意: 正如您在下面的代码中看到的,我使用 setProgress(100, downloadProgress, false)
作为省略行根本没有在通知中显示进度条.
private fun createForegroundInfo(downloadProgress: Int, videoTitle: String): ForegroundInfo {
createNotificationChannel()
val cancelText = applicationContext.getString(R.string.all_text_cancel)
val cancelPendingIntent =
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id)
val notification = NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.app_download_channel_id)
)
.setContentTitle(videoTitle)
.setTicker(videoTitle)
.setProgress(100, downloadProgress, false)
.setSmallIcon(R.drawable.ic_launcher_foreground)
.setOngoing(true)
.addAction(R.drawable.ic_close, cancelText, cancelPendingIntent)
.build()
return ForegroundInfo(notificationId, notification)
}
我也试过更新进度,如下所示,但这只会更新使用 LiveData 在 Fragment 中观察到的进度,通知中的进度条不会更新。
val lastUpdate = workDataOf("Progress" to progress)
setProgress(lastUpdate)
同样如下所示,我观察到文档提到我们应该调用 setForegroundInfo()
而不是 createForegroundInfo()
来更新正在进行的通知。但是,没有名为 setForegroundInfo()
.
如前所述,我找不到更新正在进行的通知的进度条的方法。任何帮助将不胜感激。
文档中似乎有错误,方法名称是 setForeground()
而不是 setForegroundInfo()
。我能够使用下面显示的代码更新通知中的进度。
setForeground(createForegroundInfo(progress, videoTitle))
createForegroundInfo()
private fun createForegroundInfo(downloadProgress: Int, videoTitle: String): ForegroundInfo {
createDownloadNotificationChannel()
val cancelText = applicationContext.getString(R.string.all_text_cancel)
val cancelPendingIntent =
WorkManager.getInstance(applicationContext).createCancelPendingIntent(id)
val notification = NotificationCompat.Builder(
applicationContext,
applicationContext.getString(R.string.app_download_channel_id)
)
.setContentTitle(videoTitle)
.setContentText("Downloading")
.setTicker(videoTitle)
.setProgress(100, downloadProgress, false)
.setSmallIcon(R.drawable.ic_stat_notification)
.setOngoing(true)
.addAction(R.drawable.ic_close, cancelText, cancelPendingIntent)
.build()
return ForegroundInfo(notificationId, notification)
}