每次有可用的互联网时,我都会使用以下代码来运行作业。我想要的是,在服务被触发后(由于可用的连接)我希望该服务能够定期(每30秒)继续运行g,只要有互联网,然后当连接不再可用时,服务应该停止,只有在下次有互联网时才恢复。
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本身安排,并且在时间结束后,测试是否有互联网然后好吧否则我会调用Onstop方法但是它没有办法解决这个问题。
答案 0 :(得分:1)
你可以写:setTrigger(Trigger.executionWindow(30,40))
。说明:
firebase job dispatcher 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
))