本文介绍了如何减少WorkManager中PeriodicWorkManager的时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在PeriodicTimeRequest
中,最小周期性时间为15分钟.我想将其从15分钟减少到15分钟以下.我该怎么做?
In PeriodicTimeRequest
minimum periodic time is 15 minute. I want to reduce it from 15 min to less than 15 minute.How can i do that?
推荐答案
如果间隔时间不超过15分钟.我会创建多个任务,例如:
If interval time under 15 minutes.I create multiple tasks,like that:
if (PublicStaticData.systemSet.rescueuploadinterval <= 15) {
WorkManager.getInstance(this).cancelAllWorkByTag("uploadLocationWork")
val tasks = 15 / PublicStaticData.systemSet.rescueuploadinterval
for (taskNo in 0 until tasks) {
val uploadLocationWork = PeriodicWorkRequestBuilder<UploadLocationWork>(
PublicStaticData.systemSet.rescueuploadinterval.toLong(), TimeUnit.MINUTES)
.addTag("uploadLocationWork")
.setInitialDelay((PublicStaticData.systemSet.rescueuploadinterval * taskNo).toLong(), TimeUnit.MINUTES)
.build()
WorkManager.getInstance(this).enqueue(uploadLocationWork)
Log.e("jjj", "任务${taskNo}延迟${PublicStaticData.systemSet.rescueuploadinterval * taskNo}开始")
}
} else {
// 超过15分钟,正常用就行
WorkManager.getInstance(this).cancelAllWorkByTag("uploadLocationWork")
val uploadLocationWork = PeriodicWorkRequestBuilder<UploadLocationWork>(
PublicStaticData.systemSet.rescueuploadinterval.toLong(), TimeUnit.MINUTES)
.addTag("uploadLocationWork")
.setInitialDelay(PublicStaticData.systemSet.rescueuploadinterval.toLong(), TimeUnit.MINUTES)
.build()
WorkManager.getInstance(this).enqueue(uploadLocationWork)
}
存在可接受的偏差.
这篇关于如何减少WorkManager中PeriodicWorkManager的时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!