I am using PeriodicWorkRequest to perform a task for me every 15 minutes. I would like to check, if this periodic work request has been previously scheduled. If not, schedule it.
if (!PreviouslyScheduled) {
PeriodicWorkRequest dataupdate = new PeriodicWorkRequest.Builder( DataUpdateWorker.class , 15 , TimeUnit.MINUTES).build();
WorkManager.getInstance().enqueue(dataupdate);
}
Previously when I was performing task using JobScheduler, I used to use
public static boolean isJobServiceScheduled(Context context, int JOB_ID ) {
JobScheduler scheduler = (JobScheduler) context.getSystemService( Context.JOB_SCHEDULER_SERVICE ) ;
boolean hasBeenScheduled = false ;
for ( JobInfo jobInfo : scheduler.getAllPendingJobs() ) {
if ( jobInfo.getId() == JOB_ID ) {
hasBeenScheduled = true ;
break ;
}
}
return hasBeenScheduled ;
}
Need help constructing a similar module for work request to help find scheduled/active workrequests.
Set some Tag to your PeriodicWorkRequest task:
Then check for tasks with the TAG before enqueue() work:
But if you dont really need to know that it was previously scheduled or not, you could use:
ExistingPeriodicWorkPolicy.KEEP means that the task will be scheduled only once and then work periodically even after device reboot. In case you need to re-schedule the task (for example in case you need to change some parameters of the task), you will need to use ExistingPeriodicWorkPolicy.REPLACE here
I was also searching for the same condition.I couldn't find one.So in order to solve this problem i found a mechanism.First cancel all scheduled works and reschedule the work again. So that we can ensure that only one instance of your work will be maintained. Also please be ensure that you have to maintain your worker code logic like that.
For canceling a work. For more
You need to add a unique tag to every WorkRequest. Check Tagged work.
You can group your tasks logically by assigning a tag string to any WorkRequest object. For that you need to call
WorkRequest.Builder.addTag()
Check below Android doc example:
Same you can use for
PeriodicWorkRequest
Then, You will get a list of all the WorkStatus for all tasks with that tag using
WorkManager.getStatusesByTag()
.Which gives you a
LiveData
list ofWorkStatus
for work tagged with a tag.Then you can check status using WorkStatus as below:
You can check below google example for more details. Here they added how to add a tag to WorkRequest and get status of work by tag :
https://github.com/googlecodelabs/android-workmanager
Edits Check below code and comment to how we can get WorkStatus by tag. And schedule our Work if WorkStatus results empty.
I have not tested code. Please provide your feedback for the same.
Hope this helps you.