Hassan Jamil
Hassan Jamil

Reputation: 1031

PeriodicWorkRequest is not initiating by WorkManager on realtime devices except emulator

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

Upvotes: 1

Views: 1345

Answers (3)

kanisht sahlot
kanisht sahlot

Reputation: 1

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.

Upvotes: 0

Hassan Jamil
Hassan Jamil

Reputation: 1031

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

Upvotes: 3

pfmaggi
pfmaggi

Reputation: 6476

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.

Upvotes: 1

Related Questions