如何在Firebase Job Dispatcher中取消重复任务

3

我创建了一个循环任务,当满足某些条件时,我想取消这个循环任务。

final Job.Builder builder = dispatcher.newJobBuilder()
                .setTag("myJob")
                .setService(myJobService.class)
                .setRecurring(true)
                .setTrigger(Trigger.executionWindow(30, 60));

如何在Firebase中取消任务?
1个回答

7

GitHub上的自述文件说:

Driver是一个接口,表示可以安排、取消和执行作业的组件。唯一的捆绑驱动程序是GooglePlayDriver,它依赖于内置到Google Play服务中的调度程序。

因此,取消是您使用的驱动程序的一部分。检查驱动程序接口的代码,有两种方法可以取消作业:

/**
 * Cancels the job with the provided tag and class.
 *
 * @return one of the CANCEL_RESULT_ constants.
 */
 @CancelResult
 int cancel(@NonNull String tag);

/**
 * Cancels all jobs registered with this Driver.
 *
 * @return one of the CANCEL_RESULT_ constants.
 */
 @CancelResult
 int cancelAll();

所以在您的情况下,您需要调用:
dispatcher.cancel("myJob");

或者

dispatcher.cancelAll();

调度程序将为您调用驱动程序的相应方法。如果您希望,也可以直接在驱动程序上调用方法myDriver.cancelAll(),就像GitHub项目附带的示例应用程序中所做的那样。 选择的方法将返回以下常量之一:
public static final int CANCEL_RESULT_SUCCESS = 0;
public static final int CANCEL_RESULT_UNKNOWN_ERROR = 1;
public static final int CANCEL_RESULT_NO_DRIVER_AVAILABLE = 2;

有没有任何文件提到这个? - John
不是的,我在源代码中找到了它。我稍微详细地解释了一下答案,你可以看看。 - Henning

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接