gpt4 book ai didi

Android Periodic Work Request 在应用程序启动和关闭时启动和停止

转载 作者:行者123 更新时间:2023-11-29 22:51:54 24 4
gpt4 key购买 nike

当我的应用程序启动时,我使用 WorkManager 创建了一个周期性的工作请求以从服务器获取数据并且它工作正常。我的问题是,我想在我的应用程序关闭时取消工作请求。

如何在我的应用程序关闭时取消工作请求或如何检测应用程序正在关闭?

如果有任何建议,我将不胜感激。

最佳答案

Android Documentation 中所述.

In Kotlin

WorkManager.cancelWorkById(workRequest.id)

In Java

WorkManager.cancelWorkById(workRequest.getId());

You can also cancel WorkRequests by tag using WorkManager.cancelAllWorkByTag(String). Note that this method cancels all work with this tag. Additionally, you can cancel all work with a unique name using WorkManager.cancelUniqueWork(String).

编辑

要查看应用程序关闭事件的帮助,请查看 ProcessLifecycleOwner .

Kotlin 示例

https://github.com/jshvarts/AppLifecycleDemo

关于Android Periodic Work Request 在应用程序启动和关闭时启动和停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57799263/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com