Firebase Jobdispatcher - 现在开始并重复

Posted

技术标签:

【中文标题】Firebase Jobdispatcher - 现在开始并重复【英文标题】:Firebase Jobdispatcher - Start now AND repeat 【发布时间】:2018-04-16 09:29:56 【问题描述】:

根据这个例子,我发现我现在可以使用 Trigger.NOW 或时间 0,0 开始工作:

Bundle myExtrasBundle = new Bundle();
myExtrasBundle.putString("some_key", "some_value");

Job myJob = dispatcher.newJobBuilder()
    // the JobService that will be called
    .setService(MyJobService.class)
    // uniquely identifies the job
    .setTag("my-unique-tag")
    // one-off job
    .setRecurring(false)
    // don't persist past a device reboot
    .setLifetime(Lifetime.UNTIL_NEXT_BOOT)
    // start between 0 and 60 seconds from now
    .setTrigger(Trigger.executionWindow(0, 60))
    // don't overwrite an existing job with the same tag
    .setReplaceCurrent(false)
    // retry with exponential backoff
    .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
    // constraints that need to be satisfied for the job to run
    .setConstraints(
        // only run on an unmetered network
        Constraint.ON_UNMETERED_NETWORK,
        // only run when the device is charging
        Constraint.DEVICE_CHARGING
    )
    .setExtras(myExtrasBundle)
    .build();

dispatcher.mustSchedule(myJob);

我的问题是:

如何将作业设置为现在开始,但也每隔 [时间间隔] 重复一次(我们称之为 15 分钟)

【问题讨论】:

使用 AlarmManager 并每 15 分钟启动一次此任务 我们实际上正在远离警报管理器 【参考方案1】:

要定期重复您的工作,请调用以下两种方法:

.setRecurring(true) //true mean repeat it
.setTrigger(Trigger.executionWindow(start, end))

start :被称为 windowStart,这是作业应该被认为有资格运行的最早时间(以秒为单位)。从安排作业的时间开始计算(针对新作业)

end :被称为 windowEnd,作业应该在理想世界中运行的最晚时间(以秒为单位)。计算方式与windowStart相同。

【讨论】:

我知道。但是如果我们将它设置为 15 分钟,如何让它现在开始并每 15 分钟重复一次?实际上,它会等待 15 分钟直到第一次运行 创建一个以 AlarmManager 开头的具有 executionWindow(0, 0) 的非重复作业,并在 15 分钟后创建一个具有 executionWindow(0, 15) 的重复作业

以上是关于Firebase Jobdispatcher - 现在开始并重复的主要内容,如果未能解决你的问题,请参考以下文章

Firebase jobdispatcher 未在指定窗口内触发

为啥 Firebase JobDispatcher 运行 JobService 2 次?

在 Firebase JobDispatcher 中更改定期任务的周期?

在firebase jobdispatcher中停止JobService内的计划作业

如何在 Firebase JobDispatcher 中设置周期性任务的周期?

在 Firebase JobDispatcher 和 AlarmManager 之间进行选择时感到困惑