How To Decrease Time Period Of Workmanager Android?
Solution 1:
You can make your Worker
with OneTimeWorkRequest
, and just before the closing (read returning) of it's dowWork()
, make it register itself again.
That will go like:
valtenMinuteRequest= OneTimeWorkRequestBuilder<YourWorker>()
.setInitialDelay(10, TimeUnit.MINUTES)
.build()
WorkManager.getInstance(applicationContext)
.enqueue(tenMinuteRequest)
//return Result.Success here or whatever
When you fire this Worker from another class, it will do
its work
and reschedules itself right after, and here goes the cycle.
Solution 2:
What I managed to did is a sort of hack. I called a method recursively from doWork()
. Then checked the time difference every time to make recursion stop before 15 minutes.
overridesuspendfundoWork(): Result {
startTime = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis())
doSomeWork()
return Result.success()
}
privatesuspendfundoSomeWork() {
Log.d("SomeWorker", "-------> working")
val currentTime = TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis())
val diff = currentTime - startTime
if (diff < 12L) {
Thread.sleep(30000) // this is the new time interval
doSomeWork()
}
}
This is the full worker class: https://gist.github.com/WSAyan/7f3a75d05332984e24ad5418f0b1e38b
Solution 3:
In the work manager
, there is a minimum time interval between two periodic requests is 15 minutes
.
Read this doc: https://developer.android.com/reference/kotlin/androidx/work/PeriodicWorkRequest
you can not reduce it in the work manager
.
if you want to reduce time in that case you need to use JobScheduler
it gives options with specific time intervals to execute your request.
https://developer.android.com/reference/android/app/job/JobScheduler
Solution 4:
If you need to send instantly, you can try OneTimeWorkRequest
.
Though its execution will also be dependent on the constraints you add or some other cases, but it'll start right after you call it most of the times.
Edit:
And then if you want this periodically
, You can create a PeriodicWorkRequest
and start your OneTimeWorkRequest
in it.
eg. Let's say you're having 2 methods,
startOneTimeWorker()
, startPeriodicWorker()
If you want to sync it once and instantly OneTimeWorker
will do the job.
But if you want to sync instantly and then schedule it also, call startOneTimeWorker()
, and then startPeriodicWorker()
. Where Periodic worker will be calling startOneTimeWorker()
in it's doWork()
So for the first time, OneTimeWorkRequest
will be called instantly and then it'll be called according to the schedule of PeriodicTimeWorkRequest
.
- Call
OneTimeWorkRequest
- Schedule
PeriodicWorkRequest
, callingOneTimeWorkRequest
in it
Solution 5:
If you want to send Data instantly from Mobile to Server one time then use OneTimeWorkRequestBuilder , otherwise use ForeGround Service instead of WorkManager for sending continuous data upload to server.
Both of these strategy will ensure the data upload at server side. And if it is heavy upload work which may take longer than 10 minutes , please use ForeGround Service definitely.
Post a Comment for "How To Decrease Time Period Of Workmanager Android?"