From what I read from the documentation https://developer.android.com/topic/libraries/architecture/workmanager,
It said that:
The task is still guaranteed to run, even if your app is force-quit or the device is rebooted.
So which mean, the execution that being at the background will 100% be execute until it complete no matter what?
For an example case:
An Apps have Button that perform Work Manager Implementation that upload data to an Online Database but it required Internet Connection to upload the data. So, my Apps is currently in Offline Mode and I click the Button.
My Uncertainty:
Will the Work Manager run the process at background, and keep retrying the process even when there is no Internet Connection? and only complete and stop the process until there is an Internet Connection and complete the data upload?
Will the Work Manager run the process at background, and keep retrying the process even when there is no Internet Connection? and only complete and stop the process until there is an Internet Connection and complete the data upload?
It won't implicitly try to execute the work continuously and stop only if it was successful. It will depend on Result returned by doWork()
or your Worker
. If it returns RETRY
, then the work will be retried with backoff specified in WorkRequest.Builder.setBackoffCriteria(BackoffPolicy, long, TimeUnit)
.
However, if you need something to be executed when there is internet connection, then you can specify the appropriate constraints. For network connectivity, you can set constraints as follows:
Constraints myConstraints = new Constraints.Builder()
.setRequiredNetworkType(NetworkType.CONNECTED)
.build();
OneTimeWorkRequest mywork=
new OneTimeWorkRequest.Builder(MyWorker.class)
.setConstraints(myConstraints)
.build();
WorkManager.getInstance().enqueue(mywork);
WorkManager
will ensure that your work is executed only if there is internet connection.