0

每次有可用的互联网时,我都使用以下代码运行作业。我还想要的是,在触发服务后(由于可用连接),只要有互联网,我希望该服务继续运行 g 定期(每 30 秒),然后当连接不再可用时,该服务应该停止并仅在下次有互联网时恢复。

FirebaseJobDispatcher jobDispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(MainActivity.this));

                        .setTag("JobService")

                        .setRecurring(true)

                        .setLifetime(Lifetime.FOREVER)
                        .setService(JobService.class)

                        .setTrigger(Trigger.executionWindow(0,10))

                        .setReplaceCurrent(true)
                        .setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL);    

                builder.addConstraint(Constraint.ON_UNMETERED_NETWORK);
                jobDispatcher.mustSchedule(builder.build());

我想过让 JobService 自己安排下一次运行(在 30 秒内),时间到后,测试是否有互联网然后好的,否则我会调用 Onstop 方法,但感觉不像解决这个问题的正确方法。

4

1 回答 1

1

你可以写:setTrigger(Trigger.executionWindow(30,40))。解释: firebase 作业调度程序 github

Scheduling a more complex job

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

或者对于 executionWindow,规则是:

.setTrigger(Trigger.executionWindow(
                        INTERVAL_IN_SECONDS,
                        INTERVAL_IN_SECONDS + TIME_WINDOW_IN_SECONDS
                ))

参考:https ://stackoverflow.com/a/39909986/1537413

于 2017-03-24T07:52:06.043 回答