Well as WorkManager is newly introduce in Google I/O, I'm trying to use workmanager to perform task periodically,
What I'm doing is, Scheduling the work using PeriodicWorkRequest
as following :
Constraints constraints = new Constraints.Builder().setRequiredNetworkType(NetworkType.CONNECTED).build();
PeriodicWorkRequest build = new PeriodicWorkRequest.Builder(SyncJobWorker.class, MY_SCHEDULE_TIME, TimeUnit.MILLISECONDS)
.addTag(TAG)
.setConstraints(constraints)
.build();
WorkManager instance = WorkManager.getInstance();
if (instance != null) {
instance.enqueueUniquePeriodicWork(TAG, ExistingPeriodicWorkPolicy.REPLACE, build);
}
Problem I'm having is when i'm requesting PeriodicWorkRequest
then also it will start work immediately,
I'm using : implementation "android.arch.work:work-runtime:1.0.0-alpha04"
Any help will be appreciated.
Retry and backoff policy If you require that WorkManager retry your work, you can return Result. retry() from your worker. Your work is then rescheduled according to a backoff delay and backoff policy. Backoff delay specifies the minimum amount of time to wait before retrying your work after the first attempt.
Use WorkManager for reliable work WorkManager is intended for work that is required to run reliably even if the user navigates off a screen, the app exits, or the device restarts. For example: Sending logs or analytics to backend services. Periodically syncing application data with a server.
Worker is the simplest implementation, and the one you have seen in previous sections. WorkManager automatically runs it on a background thread (that you can override).
WorkManager is not answer to all of the background tasks. E.G. You shouldn't use it for processing payments since it doesn't need to survive process death and these task needs to be executed immediately. Consider using Foreground Service. Its also not a great idea to use them for parsing data and contents of view.
You can achieve this by using flex period in PeriodicWorkRequest.Builder. As example, if you want run your Worker within 15 minutes at the end of repeat interval (let's say 8 hours), code will look like this:
val periodicRefreshRequest = PeriodicWorkRequest.Builder(
RefreshWorker::class.java, // Your worker class
8, // repeating interval
TimeUnit.HOURS,
15, // flex interval - worker will run somewhen within this period of time, but at the end of repeating interval
TimeUnit.MINUTES
)
Visual ilustration
you need to add initialDelay to the builder
fun scheduleRecurringFetchWeatherSyncUsingWorker() {
val constraints: Constraints = Constraints.Builder().apply {
setRequiredNetworkType(NetworkType.CONNECTED)
setRequiresBatteryNotLow(true)
}.build()
val request: PeriodicWorkRequest = PeriodicWorkRequest.Builder(
MySimpleWorker::class.java, 3, TimeUnit.HOURS, 1, TimeUnit.HOURS)
.setConstraints(constraints)
.setInitialDelay(2, TimeUnit.HOURS)
.setBackoffCriteria(BackoffPolicy.LINEAR, 1, TimeUnit.HOURS)
.build()
mWorkManager.enqueueUniquePeriodicWork(
TAG_WORK_NAME,
ExistingPeriodicWorkPolicy.KEEP,
request
)
}
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