Execute Task Every Second Using Work Manager Api
Solution 1:
Its not working because, the minimum interval between two periodic work request is 15 min which is defined by MIN_PERIODIC_INTERVAL_MILLIS.
Based on the documentation:
Creates a PeriodicWorkRequest to run periodically once every interval period. The PeriodicWorkRequest is guaranteed to run exactly one time during this interval. The intervalMillis must be greater than or equal to PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS. It may run immediately, at the end of the period, or any time in between so long as the other conditions are satisfied at the time.
I would recommend you to avoid scheduling work so frequently. This will end up in consuming more resources and eventually impacting the battery life.
Solution 2:
WorkManager is not designed to run tasks every second, as it has two options to build work request that is and
PeriodicWorkRequest
- runs repeated task every 15 mins, even if we change the time interval to anyhwere < 15 mins it will by default run for 15 mins only.OneTimeWorkRequest
- runs once
WorkManager will enqueues the workrequests will call the respectively Worker classes to run the task where each workerclass overrides doWork()
where the actual task is defined.
This method runs in background thread and runs for 10mins after which the worker stops.
Therefore if you want to schedule tasks running every second better the run foreground service or if your having running tasks in short duration.
If you want to run background tasks for longer periods, best practice is to avoid it.
Solution 3:
Another way to acheieve the behaviour is to create OneTimeWorkRequest
and that worker request, schedule another one for the interval you want with an initial delay
setInitialDelay(5, TimeUnit.MINUTES)
e.g.
publicclassUploadWorkerextendsWorker {
privatefinal Context context;
publicUploadWorker(
@NonNull Context context,
@NonNull WorkerParameters params) {
super(context, params);
this.context = context;
}
@Overridepublic Result doWork() {
Log.i("tracer:", "Worker executed");
// Indicate whether the work finished successfully with the ResultOneTimeWorkRequestmywork=newOneTimeWorkRequest.Builder(UploadWorker.class)
.setInitialDelay(5, TimeUnit.MINUTES)
.build();
WorkManager.getInstance(this.context).enqueue(mywork);
return Result.success();
}
}
Solution 4:
It is being so late for the answer but, work manager is useful to schedule task for periodic time for at least 15 min delay in between periodic request but somehow if you wants to achieve periodic work then you can do this with the login given below which is not a good practice but it works.
You can set worker for periodic request with 15 minutes request which will work periodically and in the worker class you can manage your worker for every second as given below.
overridesuspendfundoWork(): Result {
for (i in1..900){
delay(1000)
Log.d("Work for every second", "doWork: Running")
}
return Result.success()
}
This will work every second for 15 minutes and after 15 minutes your worker will again make a request so this is how you can achieve work for every second. You have to manage your worker to when to stop else this will create memory leaks too. This is not a best practice to work this kind of functionality for every second but this is how you can achieve this.
Post a Comment for "Execute Task Every Second Using Work Manager Api"