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

问题描述

如何检查WorkManager是否已安排.

How can I check if WorkManager is scheduled already.

这是我安排WorkManager的代码.

public static void scheduleWork() {
    PeriodicWorkRequest.Builder photoCheckBuilder =
            new PeriodicWorkRequest.Builder(WorkManagerService.class, TIME_INTERVAL_IN_SECONDS,
                    TimeUnit.SECONDS);
    PeriodicWorkRequest photoCheckWork = photoCheckBuilder.build();
    WorkManager instance = WorkManager.getInstance();
    if (instance != null) {
        instance.enqueueUniquePeriodicWork("TAG", ExistingPeriodicWorkPolicy.KEEP , photoCheckWork);
    }
}

我在Application类的onCreate()中调用scheduleWork().即使通过 此方法 ,我也可以检查我的服务是否正在运行.但是我不希望安排WorkManager,前提是已经安排了WorkManager来消除计划的时间中的不一致之处.

I call scheduleWork() in onCreate() of my Application class.Even I can check my service is running or not by this method. But I don't want schedule WorkManager if it is already scheduled to remove inconsistency in scheduled time.

if(!workManagerIsScheduled())
   {
     scheduleWork();
   }

有解决方案吗?

推荐答案

更新

如果您只是因为不想重复工作而需要检查已经在运行的工作管理器.您可以简单地使用 enqueueUniquePeriodicWork()

因此您无需担心作品的重复性.

So you don't need to worry about duplicacy about works.

 workmanager.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.KEEP , photoCheckWork);

  • TAG是唯一名称,工作经理将使用它来检查重复性.
  • 您可以 ExistingPeriodicWorkPolicy.KEEPExistingPeriodicWorkPolicy.REPLACE之间进行选择.
    • Where TAG is unique name by which work manager will check duplicacy.
    • You can choose between ExistingPeriodicWorkPolicy.KEEP and ExistingPeriodicWorkPolicy.REPLACE.
    • 我在找不到任何方法时创建了此方法.

      I created this method when I did not find any.

      private boolean isWorkScheduled(String tag) {
          WorkManager instance = WorkManager.getInstance();
          ListenableFuture<List<WorkInfo>> statuses = instance.getWorkInfosByTag(tag);
          try {
              boolean running = false;
              List<WorkInfo> workInfoList = statuses.get();
              for (WorkInfo workInfo : workInfoList) {
                  WorkInfo.State state = workInfo.getState();
                  running = state == WorkInfo.State.RUNNING | state == WorkInfo.State.ENQUEUED;
              }
              return running;
          } catch (ExecutionException e) {
              e.printStackTrace();
              return false;
          } catch (InterruptedException e) {
              e.printStackTrace();
              return false;
          }
      }
      

      if (your_work_manager.version < 1.0.0-alpha11)

      private boolean isWorkScheduled(String tag) {
          WorkManager instance = WorkManager.getInstance();
          LiveData<List<WorkStatus>> statuses = instance.getStatusesByTag(tag);
          if (statuses.getValue() == null) return false;
          boolean running = false;
          for (WorkStatus workStatus : statuses.getValue()) {
              running = workStatus.getState() == State.RUNNING | workStatus.getState() == State.ENQUEUED;
          }
          return running;
      }
      

      当某些任务为 RUNNING ENQUEUED 时,它将返回 true .

      It will return true when some of its task is RUNNING or ENQUEUED.

      public static final String TAG_MY_WORK = "mywork";
      
      if(!isWorkScheduled(TAG_MY_WORK)) { // check if your work is not already scheduled
          scheduleWork(TAG_MY_WORK); // schedule your work
      }
      
      public static void scheduleWork(String tag) {
          PeriodicWorkRequest.Builder photoCheckBuilder =
                  new PeriodicWorkRequest.Builder(WorkManagerService.class, TIME_INTERVAL_IN_SECONDS,
                          TimeUnit.SECONDS);
          PeriodicWorkRequest photoCheckWork = photoCheckBuilder.build();
          WorkManager instance = WorkManager.getInstance();
          instance.enqueueUniquePeriodicWork(tag, ExistingPeriodicWorkPolicy.KEEP , photoCheckWork);
      }
      

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

06-20 21:41