PeriodicWorkRequest is not initiating by WorkManager on realtime devices except emulator

廉价感情. 提交于 2019-12-13 03:10:40

问题


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

The dependency I have incorporated is,

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

Also

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

Code snippet,

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.

Sample Implementation: https://db.tt/gFEJi39Ofz

Google Issue Tracker link: https://issuetracker.google.com/issues/135865377


回答1:


This seems something similar to what has already been reported on some devices from this OEM. Here's a similar bug on WorkManager's issuetracker, there's not much that WorkManager can do in these cases.

As commented in this bug:

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

Your best option is to open a new issue adding some details and possibly a small sample to reproduce the issue.




回答2:


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:

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

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

Your Worker needs to be public

And by making My Worker class public I got resolved the issue.

Reference of Google's Reply on the issue: https://issuetracker.google.com/issues/135865377#comment4



来源:https://stackoverflow.com/questions/56697105/periodicworkrequest-is-not-initiating-by-workmanager-on-realtime-devices-except

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!