如何取消 firebase 作业调度程序中的重复作业

How to cancel a recurring job in firebase job dispatcher

我创建了一个循环作业,我想在满足某些条件时取消循环作业。

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

如何在 firebase 中取消作业?

GitHub 上的自述文件说:

Driver is an interface that represents a component that can schedule, cancel, and execute Jobs. The only bundled Driver is the GooglePlayDriver, which relies on the scheduler built-in to Google Play services.

所以取消是您正在使用的 driver 的一部分。检查 the code of the driver interface 有两种方法可以取消作业:

/**
 * 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();

调度员会为您调用driver对应的方法。如果需要,您也可以直接在 driver myDriver.cancelAll() like it is done in the sample app which comes with the GitHub project.

上调用这些方法

所选方法将return以下常量之一:

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;