问题
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?
回答1:
You can not change the minimum time of 15 minutes. If it can be changed to less than that then it would not be called minimum. Depending on your need try using Alarm Manager or FCM. Refer to the following link for more details: https://developer.android.com/training/efficient-downloads/regular_updates
回答2:
No, You cannot reduce the time to less than PeriodicWorkRequest.MIN_PERIODIC_INTERVAL_MILLIS
which is 15 Minutes
That's a system limitation because of the battery saving, there is no hack.
回答3:
We have a hack to reduce the 15mins time to run periodically. But you can't use PeriodicWorkRequest. Create OneTimeWorkRequest with initial delay and call self at the end of the work inside the worker. if required create the backgroundExecutor.execute {} and call from inside to make asynchronous calling. Means the worker which called self will not be terminated or onStopped() will not be called.
ExistingWorkPolicy.REPLACE | KEEP may be required depends on your need.
回答4:
A simple answer is No, You cannot reduce the time to less than MIN_PERIODIC_INTERVAL_MILLIS which is hardcoded to 15 Minutes.
However, you can test PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS which is 15 minutes.
For this, you need WorkManagerTestInitHelper
available in androidx.work.testing
.
First of all add the following dependency in the build.gradle file for your app or module:
//Current stable release is 2.3.4
androidTestImplementation "androidx.work:work-testing:2.3.4
Next, you need to use setPeriodDelayMet
method available with TestDriver
which can be used to indicate that an interval is complete and executes PeriodicWorkRequest sooner than MIN_PERIODIC_INTERVAL_MILLIS (15 minutes).
Sample code:
@Test
public void testPeriodicWork(Context context) throws Exception {
// Setup input data
Data input = new Data.Builder().put(KEY_1, 1).put(KEY_2, 2).build();
// Create periodic work request
PeriodicWorkRequest request = new PeriodicWorkRequest.Builder(MyWorker.class, 15, TimeUnit.MINUTES)
.setInputData(input)
.build();
// Enqueue periodic request
WorkManager.getInstance(context).enqueueUniquePeriodicWork(WORKER_TAG, ExistingPeriodicWorkPolicy.REPLACE, request);
// Initialize testDriver
TestDriver testDriver = WorkManagerTestInitHelper.getTestDriver();
// Tells the testing framework the period delay is met, this will execute your code in doWork() in MyWorker class
testDriver.setPeriodDelayMet(request.getId());
}
You can find more information about testing PeriodicWorkRequest at https://developer.android.com/topic/libraries/architecture/workmanager/how-to/integration-testing#periodic-work
Learn more about testing WorkManager at https://developer.android.com/reference/androidx/work/testing/WorkManagerTestInitHelper and https://developer.android.com/reference/androidx/work/testing/TestDriver
回答5:
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)
}
There is an acceptable deviation.
来源:https://stackoverflow.com/questions/55135999/how-to-reduce-time-of-periodicworkmanager-in-workmanager