android - 无法取消在前台运行的定期工作请求
问题描述
我有一个定期的工作请求在前台运行。使用以下版本WorkManager
androidx.work:work-runtime-ktx:2.5.0-alpha03
这是工作要求。
val workRequest = PeriodicWorkRequestBuilder<MyWorker>(15, TimeUnit.MINUTES)
setConstraints(constraints).build()
在工人阶级上,我将其设置为前景。
class MyWorker(context, params) : CoroutineWorker(context, params) {
override suspend fun doWork(): Result {
setForeground(createForegroundInfo())
//rest of the logic
}
private void createForegroundInfo() {
val cancelIntent = WorkManager.getInstance(context).createCancelPendingIntent(id) //work request id
val notification = NotificationCompat.Builder(context, channelId)
.setContentTitle("Test Title")
.addAction(actionIconRes, "Cancel", cancelIntent).build()
return ForegroundInfo(notificationId, notification)
}
}
当我运行它时,我可以看到带有取消操作的通知。单击后,通知会暂时消失并再次出现。进一步点击取消操作没有任何反应。也没有任何取消信号。什么可能是一个障碍?有什么线索吗?
解决方案
我想我找到了问题所在。我嵌套了导致Worker
完全取消的挂起函数。现在我已经更改了这些功能,现在一切正常。
推荐阅读
- c# - 在 Blazor 中,如何使用 EventCallback 创建接口?
- javascript - 单元组件未在 React 中重新渲染
- java - 如何在 addToSet 运算符中指定用于确定要添加的文档是否重复的键?
- bash - 在 GNU 并行中提取特定路径组件
- nginx - 自定义编译后未安装 Nginx 第三方模块指令
- reactjs - 在未使用的文件中检测到错误时如何防止出现 React 错误覆盖
- python - 背景图像在 CSS 上模糊/像素化
- windows - 如何更改 Brave/Chrome for Windows 上的窗口框架边框宽度?
- linkedin - 如何通过 LinkedIn API 获取学习者进度和所有 LinkedIn 学习路径(包括自定义路径)的课程/视频列表
- javascript - 需要导入一个普通的 javascript 文件来做出反应