PeriodicWork与enqueueUniquePeriodicWork不起作用

7 人关注

我正在尝试使用WorkManger,创建一个定期的Worker,每5天重复一次。

我在华为Android 7设备上使用1.0.0版本的workmanagers(API 24)。

android.arch.work:work-runtime:1.0.0

This is the code:

fun schedule() {
    val constraints: Constraints = Constraints.Builder().apply {
        setRequiredNetworkType(NetworkType.CONNECTED)
    }.build()
    val request = PeriodicWorkRequest
            .Builder(MyWorker::class.java, 5, TimeUnit.DAYS)
            .setConstraints(constraints)
            .build()
    WorkManager.getInstance()
            .enqueueUniquePeriodicWork(MyWorker.TAG, ExistingPeriodicWorkPolicy.KEEP, request)

MyWorker:

class MyWorker(appContext: Context, workerParams: WorkerParameters)
    : Worker(appContext, workerParams) {
    override fun doWork(): Result {
        return try {
            Thread.sleep(5000)
            Timber.i("success")
            Result.success()
        } catch (e: Exception) {
            Timber.e(e, "error")
            Result.failure()

该方法schedule()在MainActivity的onCreate()中被调用。

当我第一次安装该应用程序时,Worker只运行了一次。在日志中我还看到这个错误。 【替换代码5

我做错了什么?

2 个评论
你能看看在模拟器上用原生安卓系统运行应用程序是否有问题? 一些原始设备制造商对原生的安卓系统做了一些修改,使WorkManager无法正常运行。 是中国制造商(华为、Oppo、小米......)的支持。
Your code is perfectly fine, I tried running same on my device(One plus 5, Android 10), it's working as expected. The issue seems to on your particular device. Try updating your device OS if any update is available or try with -> android.work:work-runtime-ktx:2.4.0
android
android-jetpack
android-workmanager
smoke89
smoke89
发布于 2019-04-04
3 个回答
PerracoLabs
PerracoLabs
发布于 2020-10-11
0 人赞同

第一步是使用最新的WorkManager版本,因为你似乎仍在使用一个非常旧的版本。

https://developer.android.com/jetpack/androidx/releases/work

通过查看你的代码,你不太可能超过工作时间限制,大约是10分钟。 从文件中可以看出 导致工作被取消的一些原因是。

  • You explicitly asked for it to be cancelled (by calling WorkManager.cancelWorkById(UUID), for example).
  • In the case of unique work, you explicitly enqueued a new WorkRequest with an ExistingWorkPolicy of REPLACE. The old WorkRequest is immediately considered cancelled.
  • Your work's constraints are no longer met.
  • The system instructed your app to stop your work for some reason. This can happen if you exceed the execution deadline of 10 minutes. The work is scheduled for retry at a later time.
  • 这就是说。 他们之间的几个制造商华为 ,实施非常积极的应用杀戮,甚至不尊重调度,除非应用最近被打开。

    https://issuetracker.google.com/issues/122098785

    https://issuetracker.google.com/issues/122098785#comment41

    https://issuetracker.google.com/issues/122098785#comment74

    Furkan Yurdakul
    Furkan Yurdakul
    发布于 2020-10-11
    0 人赞同

    我不确定你得到的 CancellationException ,但你特别说过你的设备是华为设备。作为一个华为用户,我以前也遇到过同样的问题,工作第一次得到完成,15分钟后(这是我的要求),如果应用程序在后台,工作就没有执行。其原因是,华为是一个特殊的设备。你不会在stock Android或AOSP Android上观察到这种行为,因为它们允许作品被完成。然而,中国制造商,如 华为 or 小米 将强制压制工作,使工作无法完成。

    I have a 华为 P20 Lite, and the solution I've got for this is,

    Go to Settings --> Battery --> App startup (or something like that) --> in the list, find your app --> click your app or click the switch on the right --> select "Manage manually" and allow "Run in background".

    在这一调整之后,工程开始按规定执行。我很抱歉,但这与你的代码没有关系,这完全是制造商的错。

    Basu
    Basu
    发布于 2020-10-11
    0 人赞同

    来自培拉科实验室的回答。

    在独特工作的情况下,你明确地排队等候一个新的WorkRequest,其ExistingWorkPolicy为REPLACE。旧的WorkRequest被立即视为取消。

    请尽量只安排一次你的工作。你可以使用SharedPreference来检查该工作是否已经被安排。如果没有,就安排工作,并将SharedPreference设置为true。