PeriodicWorkRequest 不是由 WorkManager 在除模拟器之外的实时设备上发起的

PeriodicWorkRequest is not initiating by WorkManager on realtime devices except emulator

我正在通过 WorkManager 排队 PeriodicWorkRequest,下面提到的代码在具有 Android OS 牛轧糖的设备上也适用于 Android 的模拟器OS 版本 8.1、9 和 10 但不在 OnePlus (Android 9)、Redmi 5 (Android 8.1) 和 Google Pixel (Android 9.1) 上。

我加入的依赖是,

implementation "android.arch.work:work-runtime:1.0.1" (Non Androidx)

还有

implementation "android.arch.work:work-runtime:2.1.0-beta02" (Androidx)

代码片段,

PeriodicWorkRequest.Builder builder = new PeriodicWorkRequest.Builder(MyWorker.class,
                PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS, TimeUnit.MILLISECONDS)
            .addTag(Utils.TAG_WORKER)
            .setInputData(createInputData(config));
WorkManager.getInstance(Context).enqueueUniquePeriodicWork(Utils.TAG_WORKER, ExistingPeriodicWorkPolicy.KEEP, builder.build());

private Data createInputData(Config config) {
    return new Data.Builder()
            .putString(Utils.USER_CONFIG, new Gson().toJson(config))
            .putString(Utils.LOCATION_CONFIG, new Gson().toJson(Preferences.getInstance(fragmentActivity).getConfiguration()))
            .build();
}

我已经尝试并搜索了很多,如有任何帮助,我们将不胜感激。

示例实现: https://db.tt/gFEJi39Ofz

Google 问题跟踪器 link: https://issuetracker.google.com/issues/135865377

这似乎与该 OEM 的某些设备上已报告的情况类似。 WorkManager's issuetracker 上有一个类似的错误,在这些情况下 WorkManager 无能为力。

正如此错误中所评论的那样:

...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.

您最好的选择是打开 new issue 添加一些详细信息并可能添加一个小样本来重现该问题。

经过多次尝试,我创建了一个 issue on Google Issue Tracker under component,并将代码示例提交给团队,他们回复为:

Your Worker is package protected, and hence we cannot instantiate it using the default WorkerFactory.

如果您查看 Logcat,您会看到如下内容:

2019-06-24 10:49:18.501 14687-14786/com.example.workmanager.periodicworksample E/WM-WorkerFactory: Could not instantiate com.example.workmanager.periodicworksample.MyWorker
    java.lang.IllegalAccessException: java.lang.Class<com.example.workmanager.periodicworksample.MyWorker> is not accessible from java.lang.Class<androidx.work.WorkerFactory>
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:97)
        at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:228)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:127)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
        at java.lang.Thread.run(Thread.java:764)
2019-06-24 10:49:18.501 14687-14786/com.example.workmanager.periodicworksample E/WM-WorkerWrapper: Could not create Worker com.example.workmanager.periodicworksample.MyWorker

Your Worker needs to be public

通过让 My Worker class public 我解决了这个问题。

参考Google对该问题的回复: https://issuetracker.google.com/issues/135865377#comment4

在 Oneplus 设备上关闭电池优化。转到 phone 设置 --> 电池--> 电池优化 --> 搜索你的应用程序 --> 关闭电池优化。

Workmanager 工作正常。 Compay 与 Android 定制 Os 只是为了增加电池电量。