5

OneTimeWorkRequest当我使用 android-arch 工作 WorkManager 时,我创建了一些。

我可以WorkStatus像这样被观察者观看

final WorkManager workManager = WorkManager.getInstance();
final LiveData<List<WorkStatus>> workStatus =
    workManager.getStatusesByTag(DailyWorker.DAILY_WORK);
observer = new Observer<List<WorkStatus>>() {
  @Override public void onChanged(@Nullable List<WorkStatus> workStatuses) {
    Log.d("WorkManager", "onChanged  = workStatuses = " + workStatuses);
    if (workStatuses == null || workStatuses.size() == 0) {
      //DailyWorker.createNewPeriodWork();
    } else {
      Log.d("WorkManager ", "onChanged  = workStatuses.size() = " + workStatuses.size());
      for (int i = 0; i < workStatuses.size(); i++) {
        Log.d("WorkManager ", "onChanged Work Status Id: " + workStatuses.get(i).getId());
        Log.d("WorkManager ", "onChanged Work Status State: " + workStatuses.get(i).getState());
      }
    }
    workStatus.removeObserver(observer);
  }
};
workStatus.observe(this, observer);

我的 Android 拱门版本是android.arch.work:work-runtime:1.0.0-alpha02

WorkStatus但是名单里有很多,一些SUCCEEDED,一些ENQUEUED,一些CANCELLED,而且名单的数量还在不断增加。如何清除工作状态列表?

4

2 回答 2

18

您可以调用pruneWork()WorkManager 上的方法来清除List<WorkStatus>.

myWorkManager.pruneWork();

希望能帮助到你!

于 2018-06-29T15:17:54.553 回答
0

您可以在工作时调用 WorkRequest#keepResultsForAtLeast

于 2018-07-06T01:04:38.227 回答