Schedule updates as unique work requests

This commit is contained in:
xynngh 2021-10-30 15:01:30 +04:00
parent 13d9b0c94c
commit 66bf76e9b1
1 changed files with 7 additions and 2 deletions

View File

@ -4,6 +4,7 @@ import android.content.Context;
import android.os.Build; import android.os.Build;
import androidx.work.Constraints; import androidx.work.Constraints;
import androidx.work.ExistingPeriodicWorkPolicy;
import androidx.work.NetworkType; import androidx.work.NetworkType;
import androidx.work.PeriodicWorkRequest; import androidx.work.PeriodicWorkRequest;
import androidx.work.WorkInfo; import androidx.work.WorkInfo;
@ -19,6 +20,8 @@ import java.util.concurrent.TimeUnit;
public class UpdateScheduler { public class UpdateScheduler {
private static final String AUTO_UPDATE_WORK_TAG = "autoUpdateWork"; private static final String AUTO_UPDATE_WORK_TAG = "autoUpdateWork";
private static final String MAIN_AUTO_UPDATE_WORK_NAME = "mainAutoUpdateWork";
private static final String FREQUENT_AUTO_UPDATE_WORK_NAME = "frequentAutoUpdateWork";
private static final Logger LOG = LoggerFactory.getLogger(UpdateScheduler.class); private static final Logger LOG = LoggerFactory.getLogger(UpdateScheduler.class);
@ -46,7 +49,8 @@ public class UpdateScheduler {
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
getWorkManager().enqueue(updateRequest); getWorkManager().enqueueUniquePeriodicWork(MAIN_AUTO_UPDATE_WORK_NAME,
ExistingPeriodicWorkPolicy.REPLACE, updateRequest);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
constraints = new Constraints.Builder() constraints = new Constraints.Builder()
@ -62,7 +66,8 @@ public class UpdateScheduler {
.setConstraints(constraints) .setConstraints(constraints)
.build(); .build();
getWorkManager().enqueue(updateRequest); getWorkManager().enqueueUniquePeriodicWork(FREQUENT_AUTO_UPDATE_WORK_NAME,
ExistingPeriodicWorkPolicy.REPLACE, updateRequest);
} }
} }