gpt4 book ai didi

安卓。关闭应用程序时 WorkManager 是否正在运行?

转载 作者:IT王子 更新时间:2023-10-28 23:34:20 25 4
gpt4 key购买 nike

我想安排每晚的数据库更新。所以我使用新的 Android WorkManager。我的理解是,一旦安排好,它将始终在后台独立于应用程序的生命周期运行。那正确吗?我的第一个测试表明只有在应用程序运行时才执行工作。

val locationWork = PeriodicWorkRequest.Builder(UpdateDatabaseWorker::class.java, 24, TimeUnit.HOURS)
.addTag("DATABASE_UPDATE_SERVICE")
.build()
WorkManager.getInstance().enqueue(locationWork)

最佳答案

基于 WorkManager 上报告的各种问题 bugtracker ,他们的文档对于 WorkManager 在这种边缘情况下的确切行为并不完全准确。

On certain devices, apps are force stopped when the app is cleared from task manager, so that part is expected. ... source


Unfortunately, some devices implement killing the app from the recents menu as a force stop. Stock Android does not do this. When an app is force stopped, it cannot execute jobs, receive alarms or broadcasts, etc. So unfortunately, it's infeasible for us to address it - the problem lies in the OS and there is no workaround. source


The only issue that we have come across is the case where some Chinese OEMs treat swipe to dismiss from Recents as a force stop. When that happens, WorkManager will reschedule all pending jobs, next time the app starts up. Given that this is a CDD violation, there is not much more that WorkManager can do given its a client library. source


To add to this, if a device manufacturer has decided to modify stock Android to force-stop the app, WorkManager will stop working (as will JobScheduler, alarms, broadcast receivers, etc.). There is no way to work around this. Some device manufacturers do this, unfortunately, so in those cases WorkManager will stop working until the next time the app is launched. source


通过在具有股票 android 的 Pixel 2 XL 上对 OneTimeWorkRequest(无限制)进行密集测试,行为如下:

  • 任务管理器关闭:
    • 工作继续(稍等片刻)
  • 重启设备(工作运行):
    • 重启完成后工作继续
  • 应用信息“强制停止”:
    • 工作停止,只有在应用重新启动时才会继续
  • 重启设备(工作被“强制停止”):
    • 在应用重新启动之前,工作不会继续

您可以在 dontkillmyapp.com 上找到不同 OEM 行为的完整列表。 . Android 团队似乎也承认了这个问题,并在他们的 Android Q 的 CTS 测试中添加了一个测试。source

关于安卓。关闭应用程序时 WorkManager 是否正在运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50682061/

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