Andorid中使用WorkManager跳过的定期工作请求

问题描述 投票:0回答:1

我使用WorkManager执行了一些重复性任务,并且在白天积极使用手机时,它的工作正常(每隔一段时间或早或晚运行几分钟的代码)。在夜间,它会跳过很多时间,我想知道为什么会发生这种情况以及如何解决这个问题。

fun startService() {
    val constraintsBuilder = Constraints.Builder()
    constraintsBuilder.setRequiredNetworkType(NetworkType.CONNECTED)
    val constraints = constraintsBuilder.build()

    val work = PeriodicWorkRequest.Builder(
        HttpWorker::class.java,
        40, TimeUnit.MINUTES,
        15, TimeUnit.MINUTES
    ).setConstraints(constraints)
        .addTag("TAG_WORKER")
        .build()

    WorkManager.getInstance().enqueueUniquePeriodicWork(
        HttpWorker::class.java.simpleName,
        ExistingPeriodicWorkPolicy.REPLACE,
        work
    )
}

结果看起来像这样:

--->运行09:14

--->运行09:54(40分钟)

--->运行10:33(39分钟)

--->运行11:53(80min)

--->运行12:33(39min)

...

--->运行00:35

--->运行01:20(45分钟)

--->运行03:39(139min)!!!

--->运行07:14(215分钟)!!!

--->运行07:47(30分钟)

--->运行08:26(39分钟)

...

绝对与设备活动有关,还请注意,我禁用了我的应用程序的节能功能(我的设备是2018 Android 9.0的Samsung Galaxy新型号)。我该怎么做才能忽略夜间的许多跳过时间段(当根本不使用设备时)。

p.s。我的应用程序不在Google Play上,因此没有任何问题(但没有root用户)[]]]

我使用WorkManager实施了一些重复性任务,并且在白天积极使用手机时,它工作正常(早晚每隔几分钟运行我的代码)。在晚上的时间...

android android-workmanager
1个回答
0
投票

根据文档

© www.soinside.com 2019 - 2024. All rights reserved.