PeriodicWorkRequest with WorkManager

天大地大妈咪最大 提交于 2020-02-20 09:02:50

问题


Well as WorkManager is newly introduce in Google I/O, I'm trying to use workmanager to perform task periodically,

What I'm doing is, Scheduling the work using PeriodicWorkRequest as following :

Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
PeriodicWorkRequest build = new PeriodicWorkRequest.Builder(SyncJobWorker.class, MY_SCHEDULE_TIME, TimeUnit.MILLISECONDS)
           .addTag(TAG)
           .setConstraints(constraints)
           .build();

WorkManager instance = WorkManager.getInstance();
if (instance != null) {
          instance.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, build);
}

Problem I'm having is when i'm requesting PeriodicWorkRequest then also it will start work immediately,

  • Does anybody know how to stop that immediate work execution? while using PeriodicWorkRequest.
  • Also want to know how we can Rechedule the work? What if i want to change the time of already scheduled work ?

I'm using : implementation "android.arch.work:work-runtime:1.0.0-alpha04"

Any help will be appreciated.


回答1:


You can achieve this by using flex period in PeriodicWorkRequest.Builder. As example, if you want run your Worker within 15 minutes at the end of repeat interval (let's say 8 hours), code will look like this:

val periodicRefreshRequest = PeriodicWorkRequest.Builder(
            RefreshWorker::class.java, // Your worker class
            8, // repeating interval
            TimeUnit.HOURS,
            15, // flex interval - worker will run somewhen within this period of time, but at the end of repeating interval
            TimeUnit.MINUTES
    )

Visual ilustration




回答2:


Your code always reschedule the work, so you could change the work parameters as you want

instance.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, build);

if you want to keep the work with same parameters (even after reboot), without any changes, use:

instance.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP, build);

in the case it will be scheduled it only if the work is not engueued already.

For to stop the work task, use

// cancel the unique work
instance.cancelUniqueWork(TAG);
// clear all finished or cancelled tasks from the WorkManager 
instance.pruneWork();

Btw, I think its not good approach to use same TAG as the unigue work id and as task tag. In case you need unique work - you dont really need the task tag at all. The task tag is for cases then you want to have several tasks




回答3:


To delay the task you could use setInitialDelay.

We have used setInitialDelay to ensure the notification is triggered on the day of the event rather than immediately. This delay is calculated by a separate method which returns the amount of milliseconds between now and when the notification should trigger (at noon on the date of the event). Please note that this delay is not always accurate! Due to how modern Android works, your work can be delayed by a number of factors, so be aware of this for very time sensitive purposes.

All the info is in this post. https://medium.com/@eydryan/scheduling-notifications-on-android-with-workmanager-6d84b7f64613



来源:https://stackoverflow.com/questions/51153003/periodicworkrequest-with-workmanager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!