Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reduce time of PeriodicWorkManager in WorkManager

In PeriodicTimeRequest minimum periodic time is 15 minute. I want to reduce it from 15 min to less than 15 minute.How can i do that?

like image 622
Shubhanshu Singh Avatar asked Mar 13 '19 06:03

Shubhanshu Singh


People also ask

Does WorkManager run immediately?

You can use WorkManager to schedule immediate work for execution in the background. You should use Expedited work for tasks that are important to the user and which complete within a few minutes.

Does WorkManager run on background thread?

Worker is the simplest implementation, and the one you have seen in previous sections. WorkManager automatically runs it on a background thread (that you can override).

Does WorkManager work in background?

WorkManager is part of Android Jetpack and an Architecture Component for background work that needs a combination of opportunistic and guaranteed execution. Opportunistic execution means that WorkManager will do your background work as soon as it can.

How do I stop WorkManager?

Cancelling and stopping work If you no longer need your previously enqueued work to run, you can ask for it to be cancelled. Work can be cancelled by its name , id or by a tag associated with it. workManager. cancelAllWorkByTag("syncTag");


4 Answers

A simple answer is No, You cannot reduce the time to less than MIN_PERIODIC_INTERVAL_MILLIS which is hardcoded to 15 Minutes.

However, you can test PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS which is 15 minutes.

For this, you need WorkManagerTestInitHelper available in androidx.work.testing.

First of all add the following dependency in the build.gradle file for your app or module:

//Current stable release is 2.3.4
androidTestImplementation "androidx.work:work-testing:2.3.4

Next, you need to use setPeriodDelayMet method available with TestDriver which can be used to indicate that an interval is complete and executes PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS (15 minutes). Sample code:

@Test
public void testPeriodicWork(Context context) throws Exception {
    // Setup input data
    Data input = new Data.Builder().put(KEY_1, 1).put(KEY_2, 2).build();

    // Create periodic work request
    PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(MyWorker.class, 15,  TimeUnit.MINUTES)
                                       .setInputData(input)
                                       .build();
    // Enqueue periodic request
    WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, request);

    // Initialize testDriver
    TestDriver testDriver = WorkManagerTestInitHelper.getTestDriver();

    // Tells the testing framework the period delay is met, this will execute your code in doWork() in MyWorker class
    testDriver.setPeriodDelayMet(request.getId());

}

You can find more information about testing PeriodicWorkRequest at https://developer.android.com/topic/libraries/architecture/workmanager/how-to/integration-testing#periodic-work

Learn more about testing WorkManager at https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper and https://developer.android.com/reference/androidx/work/testing/TestDriver

like image 136
Dr. DS Avatar answered Oct 04 '22 18:10

Dr. DS


We have a hack to reduce the 15mins time to run periodically. But you can't use PeriodicWorkRequest. Create OneTimeWorkRequest with initial delay and call self at the end of the work inside the worker. if required create the backgroundExecutor.execute {} and call from inside to make asynchronous calling. Means the worker which called self will not be terminated or onStopped() will not be called.

ExistingWorkPolicy.REPLACE | KEEP may be required depends on your need.

like image 23
Praveen Balaji Avatar answered Oct 04 '22 20:10

Praveen Balaji


No, You cannot reduce the time to less than PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS which is 15 Minutes That's a system limitation because of the battery saving, there is no hack.

like image 43
Hamza Ezzaydia Avatar answered Oct 04 '22 19:10

Hamza Ezzaydia


I found this solution: https://ariefbayu.xyz/execute-periodic-process-under-15-minutes-cycle-using-workmanager-564c3ade94c4

public Result doWork() {

    try {
        Log.d("PWLOG", "Let me sleep a moment...");
        Thread.sleep(60000 * 5);//5 minutes cycle
        doTheActualProcessingWork();
    } catch (InterruptedException e) {
        Log.d("PWLOG", "Thread sleep failed...");
        e.printStackTrace();
    }
    return Result.success();
}

It seems to work in a correct way

like image 29
aflopezbec Avatar answered Oct 04 '22 18:10

aflopezbec