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.
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
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.
beginUniqueWork()
returns a WorkContinuation
object. You need to call enqueue
on that WorkContinuation
to actually enqueue it with WorkManager:
workManager.beginUniqueWork(
SyncWorker.TAG,
ExistingWorkPolicy.REPLACE,
oneTimeWorkRequest
).enqueue();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With