When will the oneOffTaks be executed again with result GcmNetworkManager.RESULT_RESCHEDULE

82 views Asked by At

So from Android SDK for GcmNetworkManager

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);

Inside MyService

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

When the execution fails, it will return RESULT_RESCHEDULE, and it will be retried. So I am wondering when will it be retried?

Thanks

1

There are 1 answers

1
abielita On

Based from this documentation, RESULT_RESCHEDULE means that your task failed then reschedules your task, so it will be executed again when conditions are met.

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

However, you may check this tutorial when your code often fails and must be retried. Java 7/8 library provides rich and unobtrusive API with fast and scalable solution to this problem.

Hope this helps!