Ashish Khurange
Ashish Khurange

Reputation: 923

Android Oreo workmanager PeriodicWork not working

Android version: 8.1.0

Device: Infinix X604B

Workmanager: 1.0.0-alpha11 (latest version)

Scheduled a PeriodicWorkRequest to run after every 15 minutes. Work request runs after every 15 minutes for approximately one hour. Then PeriodicWorkRequest stops working. In 8 hours the background work doesn't get scheduled at all. I have not killed my app, it is in the background.

When I bring the app back to foreground PeriodicWorkRequest runs the background task again. With similar experience no repetition after I put my app in the background.

I've disabled battery optimization for my app.

Here is My Worker class sample.

class TestWorker extends Worker {
public TestWorker(
        @NonNull Context context,
        @NonNull WorkerParameters params) {
    super(context, params);
}

@NonNull
@Override
public Result doWork() {
    // Adding timestamp of background execution to firestore.
    Map<String, String> value = new TreeMap<>();
    SimpleDateFormat df = new SimpleDateFormat("hh:mm");
    String dateFormat = df.format(Calendar.getInstance().getTime());
    value.put("time", dateFormat);
    FirebaseFirestore.getInstance()
            .collection("my-collection")
            .add(value);
    return Result.SUCCESS;
}

}

This is how I call it:

PeriodicWorkRequest.Builder testBuilder =
    new PeriodicWorkRequest.Builder(TestWorker.class, 15,
                                    TimeUnit.MINUTES);

PeriodicWorkRequest testWork = testBuilder.build();
WorkManager.getInstance().enqueue(testWork);

Upvotes: 8

Views: 4854

Answers (2)

Rajesh Naddy
Rajesh Naddy

Reputation: 1105

I worked with PeriodicWorkRequest recently and got to know that Workmanager is still in Alpha/Beta mode in android.arch and has some Bugs which is yet to fixed. Then i gone through the documentation about androidX (this) where the stable version is released and it is working Perfectly as expected.The major concern is, you have to Migrate your project to AndroidX.

In simple words, if you use "android.arch.work:work-runtime:1.x.x" no guarantee of working as of now(until we have a stable release). Try with "androidx.work:work-runtime" which will work definitely.

Go through the doc for further reference. this and this.

NOTE::

If you want to migrate your project to AndroidX. Follow these steps,

Android Studio(v3.4.1) Toolbar -> Refactor -> Migrate to AndroidX

Upvotes: 0

Jeel Vankhede
Jeel Vankhede

Reputation: 12138

You can begin with unique periodic work like below,

WorkManager.getInstance().enqueueUniquePeriodicWork(UNIQUE_ID, ExistingPeriodicWorkPolicy.REPLACE, testWork);

Here, UNIQUE_ID is String that will check for uniqueness of your worker and replace existing one if it's already exists in queue for periodic work.

Check out Worker policies here.

And don't forget to add the same tag to your worker instance like:

PeriodicWorkRequest.Builder testBuilder =
new PeriodicWorkRequest.Builder(TestWorker.class, 15, TimeUnit.MINUTES);
PeriodicWorkRequest testWork = testBuilder.addTag(UNIQUE_ID).build(); // Set the same string tag for worker.

Upvotes: 1

Related Questions