Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Periodic daily work requests using WorkManager

How to properly use the new WorkManager from Android Jetpack to schedule a one per day periodic work that should do some action on a daily basis and exactly one time?

The idea was to check if the work with a given tag already exists using WorkManager and to start a new periodic work otherwise.

I've tried to do it using next approach:

public static final String CALL_INFO_WORKER = "Call worker";

...

WorkManager workManager = WorkManager.getInstance();
List<WorkStatus> value = workManager.getStatusesByTag(CALL_INFO_WORKER).getValue();
if (value == null) {
    WorkRequest callDataRequest = new PeriodicWorkRequest.Builder(CallInfoWorker.class,
                24, TimeUnit.HOURS, 3, TimeUnit.HOURS)
                .addTag(CALL_INFO_WORKER)
                .build();
    workManager.enqueue(callDataRequest);
}

But the value is always null, even if I put a breakpoint inside the Worker's doWork() method (so it is definitely in progress) and check the work status from another thread.

like image 520
Gaket Avatar asked May 15 '18 18:05

Gaket


People also ask

Can WorkManager be used to repeat jobs?

Retry and backoff policyIf you require that WorkManager retry your work, you can return Result. retry() from your worker. Your work is then rescheduled according to a backoff delay and backoff policy. Backoff delay specifies the minimum amount of time to wait before retrying your work after the first attempt.

When would you use a WorkManager?

Use WorkManager for reliable workWorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.

What is difference between WorkManager and service?

WorkManager is not answer to all of the background tasks. E.G. You shouldn't use it for processing payments since it doesn't need to survive process death and these task needs to be executed immediately. Consider using Foreground Service. Its also not a great idea to use them for parsing data and contents of view.

What is the best tool for handling work that is deferrable?

WorkManager is an Android Jetpack library that runs deferrable, guaranteed background work when the work's constraints are satisfied. It is the current best practice for this kind of work on Android.


2 Answers

You are looking for enqueueUniquePeriodicWork

This method allows you to enqueue a uniquely-named PeriodicWorkRequest, where only one PeriodicWorkRequest of a particular name can be active at a time. For example, you may only want one sync operation to be active. If there is one pending, you can choose to let it run or replace it with your new work.

Sample code

public static final String TAG_MY_WORK = "mywork";

public static void scheduleWork(String tag) {
    PeriodicWorkRequest.Builder photoCheckBuilder =
            new PeriodicWorkRequest.Builder(WorkManagerService.class, 1, TimeUnit.DAYS);
    PeriodicWorkRequest request = photoCheckBuilder.build();
    WorkManager.getInstance().enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP , request);
}

You get two types of ExistingPeriodicWorkPolicy

KEEP

If there is existing pending work with the same unique name, do nothing.

REPLACE

If there is existing pending work with the same unique name, cancel and delete it.

like image 29
Khemraj Sharma Avatar answered Sep 21 '22 20:09

Khemraj Sharma


You can now use enqueueUniquePeriodicWork method. It was added in 1.0.0-alpha03 release of the WorkManager.

like image 62
Greg Dan Avatar answered Sep 22 '22 20:09

Greg Dan