问题描述
我正在通过 WorkManager
使 PeriodicWorkRequest
入队,下面提到的代码也在具有Android OS Nougat的设备上运行在Android OS 8.1、9及更高版本的模拟器上10,但不能在OnePlus(Android 9),Redmi 5(Android 8.1)& Google Pixel(Android 9.1)。
I am enqueuing a PeriodicWorkRequest
through WorkManager
, the code mentioned below is working on device having Android OS Nougat also on emulators of Android OS versions 8.1, 9 & 10 but not on 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();
}
我已经尝试和搜索了很多,有关的任何帮助将不胜感激。
I have tried and searched a lot, any help regarding will be much appreciated.
示例实现:
Google Issue Tracker链接:
Google Issue Tracker link:https://issuetracker.google.com/issues/135865377
推荐答案
之后如此多的尝试,我在Google问题跟踪工具的问题 ://issuetracker.google.com/issues?q = componentid:409906 rel = nofollow noreferrer>组件,还向团队提交了代码示例,他们的回复是:
After so many tries, I have created an issue on Google Issue Tracker under component, also submitted the code sample to the team and they replied as:
如果您查看Logcat,您会看到类似的东西:
If you looked at Logcat, you would see something like:
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
通过公开我的工人班,我解决了这个问题。
And by making My Worker class public I got resolved the issue.
有关此问题的Google答复的引用:
Reference of Google's Reply on the issue:https://issuetracker.google.com/issues/135865377#comment4
这篇关于除模拟器以外,WorkManager不会在实时设备上启动PeriodicWorkRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!