应用程序关闭后,WorkManager 停止调度周期性 Worker

WorkManager stops scheduling periodic Worker after the app closes

也许这是关于 WorkManager 的另一个问题,但我真的找不到解决方案...

正如标题所示,我正在尝试 运行 每 15 分钟进行一次周期性工作。实际上,在工作人员中,我每分钟都在轮询一些数据。每次轮询后,我几乎每隔 1 秒检查一次工作人员是否正在停止,如果是 return,否则继续等待直到达到 1 分钟并再次轮询数据。

根据文档,这应该有效,而且确实如此,直到我从最近的应用程序屏幕中终止该应用程序。

代码如下:

package com.qsea.app.cordova;

import android.content.Context;

import android.os.SystemClock;

import android.util.Log;

import androidx.work.Worker;
import androidx.work.WorkerParameters;

public class ServerListenerWorker extends Worker {

    public static final String TAG = "ServerListenerWorker";

    public ServerListenerWorker(
        Context appContext,
        WorkerParameters workerParams
    ) {
        super(appContext, workerParams);
    }

    @Override
    public Result doWork() {

        Log.d(TAG, "Doing work");

        final long startTime = SystemClock.elapsedRealtime();
        final int maxDelta = 840000; // 14 minutes

        while (true) {
            // I did this to stop this worker until 15 minutes
            // and then let the next worker run
            if (SystemClock.elapsedRealtime() - startTime >= maxDelta) {
                break;
            }

            // Here I'm polling data, if the polling results in a failure
            // I return Result.retry()

            // It avoid waiting if it remains only 1 minute until the max time
            if (SystemClock.elapsedRealtime() - startTime >= (maxDelta - 60000)) {
                break;
            }
            for (int i = 0; i < 60; i++) {
                SystemClock.sleep(950);
                // Here it checks if it is stopped
                if (isStopped()) {
                    Log.d(TAG, "Detected stop"); // this is actually never reached
                    break;
                }
            }
        }
        return Result.success();
    }
}

然后我开始工作

Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
PeriodicWorkRequest periodicWorkRequest = new PeriodicWorkRequest.Builder(ServerListenerWorker.class, 15, TimeUnit.MINUTES)
    .addTag(serverListenerWorkerUID)
    .setConstraints(constraints)
    .build();
WorkManager.getInstance(cordova.getActivity()).enqueueUniquePeriodicWork(serverListenerWorkerUID, ExistingPeriodicWorkPolicy.KEEP, periodicWorkRequest);

我通过查看我的服务器日志来检查工作人员是否正常工作(如果我收到请求,它工作正常)并且它似乎工作正常,直到我关闭应用程序。然后它停止 运行 实际工作人员并且再也不会 运行 工作人员,直到我重新打开应用程序,工作似乎已排队并在应用程序打开后立即恢复。

我是不是初始化有问题? 我的 AndroidManifest.xml

中也有 <service android:name=".ServerListenerWorker" android:permission="android.permission.BIND_JOB_SERVICE" />

这是预期的行为吗?

我听说中文ROM对后台服务有额外的限制,我有一个华为,这似乎是最糟糕的。那么它可能是设备吗?如果是这样,Facebook、WhatsApp、Instagram 和其他公司如何管理它以在这些设备上运行?

让我们把它分成两个不同的问题。

  1. 你在你的 worker 中做的事情很耗电,如果你使用前台服务通知用户你的应用程序在后台持续 运行 会更好。您还可以将 WorkManager 与新引入的对 long-运行 worker 的支持一起使用(在后台使用前台服务)。 More on this in the documentation.

  2. 如果特定 OEM 有问题,请open an issue on the Android issuetracker as this maybe a CDD violation. Google can contact the OEM and request that they fix the ROM. This is going to take time, in the meanwhile, you can take a look at sites like don't kill my app to understand what are the constraints on a specific device and use a library like autostarter帮助用户找到正确的设置。

顺便说一句,您不需要在 AndroidManifest.xml 文件中列出您的工作人员