Correctly cancel periodic work request by id not by tag. Add an initial delay which is the same time as the interval when start PullNotificationWorker in NotificationPreferenceFragment.

This commit is contained in:
Alex Ning 2019-08-22 17:30:42 +08:00
parent d864ec6d7f
commit 23c76a86cb
2 changed files with 9 additions and 7 deletions

View File

@ -76,12 +76,13 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, TimeUnit.HOURS)
.setConstraints(constraints)
.setInitialDelay(notificationInterval, TimeUnit.HOURS)
.build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
} else {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG);
}
return true;
});
@ -100,12 +101,13 @@ public class NotificationPreferenceFragment extends PreferenceFragmentCompat {
new PeriodicWorkRequest.Builder(PullNotificationWorker.class,
notificationInterval, TimeUnit.HOURS)
.setConstraints(constraints)
.setInitialDelay(notificationInterval, TimeUnit.HOURS)
.build();
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
ExistingPeriodicWorkPolicy.REPLACE, pullNotificationRequest);
} else {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG);
}
return true;

View File

@ -319,7 +319,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG);
}
bindView();
@ -345,7 +345,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG);
}
bindView();
@ -375,7 +375,7 @@ public class MainActivity extends AppCompatActivity implements SortTypeBottomShe
workManager.enqueueUniquePeriodicWork(PullNotificationWorker.WORKER_TAG,
ExistingPeriodicWorkPolicy.KEEP, pullNotificationRequest);
} else {
workManager.cancelAllWorkByTag(PullNotificationWorker.WORKER_TAG);
workManager.cancelUniqueWork(PullNotificationWorker.WORKER_TAG);
}
bindView();