A
A
Artem Sorokin2021-12-17 23:15:35
Java
Artem Sorokin, 2021-12-17 23:15:35

Why doesn't WorkManager work?

I'm trying to get the WorkManager to periodically launch the Logger.java service I wrote.

In MainActivity.java added to the onCreate method:

protected void onCreate(Bundle savedInstanceState) {
...
     WorkManager.getInstance().enqueue(myWorkRequest);
     PeriodicWorkRequest workRequest = new PeriodicWorkRequest.Builder(Logger.class, 15, TimeUnit.MINUTES, 15, TimeUnit.MINUTES).addTag("Logger").build();
     WorkManager.getInstance().enqueueUniquePeriodicWork("Logger", ExistingPeriodicWorkPolicy.KEEP, workRequest);
...
}

When I open the application and keep it open while the screen is running, everything works properly, once every 15-30 minutes the Logger service works.

I block the device, unblock it in an hour, go into the application - the Logger service normally works out 2-4 times in a row (the number of missed WorkManager hits, obviously).

But I need the triggers to occur in the background, it doesn’t matter if the screen is unlocked or not, the application is open on the screen or not.

What can I be missing?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
AlexVWill, 2021-12-17
@AlexVWill

Hypothesis: you run WorkManager as part of the Android UI process, that's where the problem comes from, it is possible that after the system kills the idle process, the WorkManager also stops (possibly with an error), because. he is part of the process.
Try running WorkManager as an asynchronous task or Thread.
There are some smart ideas here: microsin.net/programming/android/processes-and-thr...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question