本文介绍了检查WorkRequest先前是否已被WorkManager包围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PeriodicWorkRequest每15分钟执行一次任务.我想检查一下是否定期安排了这个定期工作请求.如果没有,请安排它.

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);
      }

以前,当我使用JobScheduler执行任务时,我曾经使用

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.

推荐答案

为您的PeriodicWorkRequest任务设置一些标记:

Set some Tag to your PeriodicWorkRequest task:

    PeriodicWorkRequest work =
            new PeriodicWorkRequest.Builder(DataUpdateWorker.class, 15, TimeUnit.MINUTES)
                    .addTag(TAG)
                    .build();

然后在enqueue()工作之前检查带有TAG的任务:

Then check for tasks with the TAG before enqueue() work:

    WorkManager wm = WorkManager.getInstance();
    ListenableFuture<List<WorkStatus>> future = wm.getStatusesByTag(TAG);
    List<WorkStatus> list = future.get();
    // start only if no such tasks present
    if((list == null) || (list.size() == 0)){
        // shedule the task
        wm.enqueue(work);
    } else {
        // this periodic task has been previously scheduled
    }

但是,如果您真的不需要知道它是否先前已安排,则可以使用:

But if you dont really need to know that it was previously scheduled or not, you could use:

    static final String TASK_ID = "data_update"; // some unique string id for the task
    PeriodicWorkRequest work =
            new PeriodicWorkRequest.Builder(DataUpdateWorker.class,
                    15, TimeUnit.MINUTES)
                    .build();

    WorkManager.getInstance().enqueueUniquePeriodicWork(TASK_ID,
                ExistingPeriodicWorkPolicy.KEEP, work);

ExistingPeriodicWorkPolicy.KEEP意味着该任务仅计划一次,然后即使在设备重新启动后也可以定期工作.如果您需要重新安排任务的时间(例如,如果您需要更改任务的某些参数),则需要在此处使用ExistingPeriodicWorkPolicy.REPLACE

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

这篇关于检查WorkRequest先前是否已被WorkManager包围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 10:12