From 23c76a86cb0ccb71776c13bd619155d35797a26e Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Thu, 22 Aug 2019 17:30:42 +0800 Subject: 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. --- app/src/main/java/Settings/NotificationPreferenceFragment.java | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'app/src/main/java/Settings/NotificationPreferenceFragment.java') diff --git a/app/src/main/java/Settings/NotificationPreferenceFragment.java b/app/src/main/java/Settings/NotificationPreferenceFragment.java index 17a5d07a..4d9051fe 100644 --- a/app/src/main/java/Settings/NotificationPreferenceFragment.java +++ b/app/src/main/java/Settings/NotificationPreferenceFragment.java @@ -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; -- cgit v1.2.3