Android Oreo workmanager PeriodicWork not working
Asked Answered
C

2

8

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);
Chloric answered 13/11, 2018 at 9:9 Comment(6)
Can you share code about how you start your PeriodicWorker?Interject
I too had like this problem. I have tried with Xiomi phone. Due to custom rom work manger didn't work well. So i have started to use Alarm Manager for this type of function.Romance
@JeelVankhede Added the code sample.Chloric
@Romance will give a try for Alarm Manager and update you. Thanks.Chloric
@AshishKhurange Did you make it work? In my case, dowork only trigger one time and never repeat even if I set the time to 1 second!Weltschmerz
@Weltschmerz No when app goes in background on that particular device periodic work never repeated. But when app comes in foreground, periodic work is launched. Note that min time to repeat is 15 minutes, you can not set 1 second.Chloric
I
1

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.
Interject answered 15/11, 2018 at 5:17 Comment(6)
No luck with your advice. On my device Workmanager doesn't run as expected. Even when I'm using my device, if the App is in standby mode Workmanager doesn't schedule the task. Workmanager works only when my app is in use.Chloric
It might be because if you've enabled don't keep activities from device developer options. (It's just a guess)Interject
the same issue with me just like @AshishKhurange. I am doing the 100% sameDevlin
@AshishKhurange : Did u find a solution? I m facing same issuePyrethrum
I would suggest to use latest artifact with androidx version.Interject
@Pyrethrum I found no solution. Check ou this link many more face the same issue, for background execution, alarm manager and even FCM: dontkillmyapp.com/problemChloric
C
0

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

Chemosynthesis answered 23/7, 2019 at 18:50 Comment(1)
Thanks for the suggestion. Let me try it out. Will update you soon.Chloric

© 2022 - 2024 — McMap. All rights reserved.