安卓.关闭应用程序后,WorkManager是否正在运行?

安卓.关闭应用程序后,WorkManager是否正在运行?

问题描述:

我想安排每晚进行数据库更新.因此,我使用了新的Android WorkManager.我的理解是,一旦安排好,它将始终在后台运行,而与应用程序的生命周期无关. 是对的吗?我的第一个测试表明,仅在应用程序运行时才执行Work.

I want to schedule nightly database updates. So I use new Android WorkManager. My understanding is that once scheduled it will always run in the background independently from the app's lifecycle. Is that right? My first tests show that Work is only being performed when the app is running.

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

基于 WorkManager 报告的各种问题

Based on various issues reported on the WorkManager bugtracker, their documentation is not completely precise about the exact behavior of the WorkManager in such edge cases.

在某些设备上,当从任务管理器中清除应用程序时,将强制停止应用程序,因此该部分是预期的. ... 来源


不幸的是,某些设备实施了从最近"菜单中杀死应用程序的强制停止操作. Stock Android不会执行此操作.当应用程序被强制停止时,它无法执行作业,接收警报或广播等.因此,不幸的是,对于我们来说,解决它是不可行的-问题出在操作系统上,没有解决方法. 来源


我们遇到的唯一问题是,某些中国OEM厂商将刷卡从最近的员工"中解雇为强制停止的情况.发生这种情况时,WorkManager将在下次启动该应用程序时重新计划所有待处理的作业.鉴于这是对CDD的违反,给定其客户端库,WorkManager所能做的事情就不多了. 来源

此外,如果设备制造商决定修改库存的Android以强制停止应用程序,则WorkManager将停止工作(JobScheduler,警报,广播接收器等也将停止工作).没有办法解决此问题.不幸的是,某些设备制造商会这样做,因此在这种情况下,WorkManager将停止工作,直到下次启动该应用程序为止. 来源

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(无约束)进行了严格测试,其行为如下:


With intense testing of a OneTimeWorkRequest (without constraints) on a Pixel 2 XL with stock android the behavior is the following:

  • 任务管理器关闭:
    • 工作继续(稍后)
    • 重新启动完成后,工作会继续
    • 工作停止,只有在再次启动应用程序后才会继续
    • 在重新启动应用程序之前,工作不会继续

    您可以在 dontkillmyapp.com 上找到不同OEM行为的完整列表.似乎Android团队也承认了这个问题,并为此在Android Q的CTS测试中添加了一个测试. "rel =" noreferrer>来源

    You can find a complete list of different OEM behaviors on dontkillmyapp.com. It seems the Android team also acknowledges this issue and added a test for this into their CTS test for Android Q. source