Currently, I'm using WorkManager 1.0.0-alpha02.
def work_version = "1.0.0-alpha02"
implementation "android.arch.work:work-runtime:$work_version" // use -ktx for Kotlin
// optional - Firebase JobDispatcher support
implementation "android.arch.work:work-firebase:$work_version"
I have no problem to execute background worker, by using the following code, when the app quit.
Use enqueue, work as expected
OneTimeWorkRequest oneTimeWorkRequest =
new OneTimeWorkRequest.Builder(SyncWorker.class)
.addTag(SyncWorker.TAG)
.build();
WorkManager workManager = WorkManager.getInstance();
workManager.enqueue(oneTimeWorkRequest);
Since, I would like to avoid more than one SyncWorker
running at the same time. I try to use
Use beginUniqueWork, doesn't work
OneTimeWorkRequest oneTimeWorkRequest =
new OneTimeWorkRequest.Builder(SyncWorker.class)
.addTag(SyncWorker.TAG)
.build();
WorkManager workManager = WorkManager.getInstance();
workManager.beginUniqueWork(
SyncWorker.TAG,
ExistingWorkPolicy.REPLACE,
oneTimeWorkRequest
);
SyncWorker
is not running at all.
May I know what step I had missed out? Thank you.
ExistingWorkPolicy.KEEP
, and purposely make the worker doing infinity while loop with sleep. No more than 1 worker running. However, if I explicitly performcancelAllWorkByTag(SyncWorker.TAG)
andcancelUniqueWork(SyncWorker.TAG)
before 2nd enqueue, I notice previous worker will not be killed. Yet, 2nd worker is allowed to run. This makes 2 workers running simultaneously. Does this mean, if we are using beginUniqueWork, we shouldn't callcancelAllWorkByTag
andcancelUniqueWork
? – Refractor