gpt4 book ai didi

android - 何时会再次执行 oneOffTaks 结果 GcmNetworkManager.RESULT_RESCHEDULE

转载 作者:太空狗 更新时间:2023-10-29 13:10:12 27 4
gpt4 key购买 nike

因此来自 GcmNetworkManager 的 Android SDK

public static final int RESULT_RESCHEDULE

Indicates a task has failed to execute, and must be retried with back-off.

Task task = new OneoffTask.Builder()
.setService(MyService.class)
.setExecutionWindow(0, 15)
.setUpdateCurrent(true)
.setRequiredNetwork(Task.NETWORK_STATE_CONNECTED)
.setRequiresCharging(false)
.build();
mGcmNetworkManager.schedule(task);

在我的服务中

public int onRunTask(TaskParams taskParams) {
/** task execution logic here */
if (success) {
return RESULT_SUCCESS;
} else {
return RESULT_RESCHEDULE;
}
}

当执行失败时,会返回RESULT_RESCHEDULE,会重试。所以我想知道什么时候会重试?

谢谢

最佳答案

基于此documentation , RESULT_RESCHEDULE 意味着你的任务失败然后重新安排你的任务,所以它会在满足条件时再次执行。

By default, if you return RESULT_RESCHEDULE from your onRunTask, your task is executed again as soon as conditions (charging, internet available) you defined for it are met.

  • one option, if you need only to limit the count of reschedules is to store somewhere the number of reschedules, and when it reaches your defined limit, return RESULT_FAILURE instead of RESULT_RESCHEDULE
  • second one, if you need more control would be to return RESULT_FAILURE and schedule an OneOff task, make it recognizable by some special tag and again count how many times have you scheduled it somewhere, this way you will be able to control count and delay between executions of the task

但是,您可以检查这个 tutorial当您的代码经常失败并且必须重试时。 Java 7/8 库提供了丰富且不显眼的 API 以及针对此问题的快速且可扩展的解决方案。

希望这对您有所帮助!

关于android - 何时会再次执行 oneOffTaks 结果 GcmNetworkManager.RESULT_RESCHEDULE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42081143/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com