PeriodicWorkRequest is not initiating by WorkManager on realtime devices except emulator
Asked Answered
Z

3

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

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

Zadoc answered 21/6, 2019 at 5:1 Comment(7)
what is value for PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS?Passmore
As per official documentation its value is 15 minutes or 900000 ms: developer.android.com/reference/androidx/work/…Zadoc
Is it at least running for first time?Ji
No the worker is not running even for the first time.Zadoc
Did you try with interval 15 mins and TimeUnit as MinutesBiserrate
Yes I have also tried for it (minutes), even I tried for a greater value.Zadoc
Also I have migrated the project to Androidx, and facing the same behavior.Zadoc
Z
3

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

Zadoc answered 25/6, 2019 at 6:2 Comment(1)
Strange, I never knew that the Worker class needs to be declared public... ! It seems to have been working fine for me even without this public declaration, until I noticed an Exception like you state... for the first time today... despite having been using WorkManager ever since its inception! Thanks for your post!Limbic
C
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.

Communalize answered 23/6, 2019 at 17:45 Comment(1)
Thank you for your help. I have created one here: issuetracker.google.com/issues/135865377Zadoc
M
0

On Oneplus devices turn off battery optimization. Go to phone setting --> battery--> bettery optimization --> search your app --> turn off batter optimization.

Workmanager works properly. Compay's just mesh up with Android custom Os just to increase their battery back.

Magnet answered 18/10, 2020 at 14:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.