I using WorkManager instead of AlarmManager and i want to update a request at runtime so i need to retrieve that request. How can i do that?
OneTimeWorkRequest request = new OneTimeWorkRequest.Builder(TripWorker.class)
.setInputData(dataBuilder.build())
.setInitialDelay(milliseconds, TimeUnit.MILLISECONDS)
.addTag("Tag")
.build();
workManager.enqueue(request);
You cannot modify an existing WorkRequest.
You can use WorkManager's unique work and enqueue a modified WorkRequest to replace the existing one:
enqueueUniqueWork
instead of using enqueue
UniqueWorkRequest
, using the same unique name, with a REPLACE
policy.Keep in mind that the your Worker maybe running while you replace it. You need to carefully handle stoppages in your Worker (stoppages are cooperative, so you need to add some code as explained in the guide) using the isStopped
method and implementing the onStopped()
callback.