0

我已经配置了如下的工作:

FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
    Job myJob = dispatcher.newJobBuilder()
            .setService(InfoJobService.class)
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(0, 10))
            .setLifetime(Lifetime.FOREVER)
            .setTag("data")
            .setReplaceCurrent(true)
            .setConstraints(Constraint.ON_ANY_NETWORK)
            .build();
    dispatcher.mustSchedule(myJob);

在 JobService 类中,我修改了 onstart 和 onstop 方法,如下所示:

 @Override
public boolean onStartJob(JobParameters job) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            uploadDatToFirebase();
        }
    }).start();
    return true;
}
@Override
public boolean onStopJob(JobParameters job) {
    return true;
}

但是工作调度员只工作了一次,它不会重复出现。我的问题与这个问题相似, 但那里没有给出解决方案,所以我不得不再次询问。

4

1 回答 1

1

我不知道解决方案是否适合每种情况,但它对我有用。 工作服务:

@Override
public boolean onStartJob(final JobParameters job) {
    new Thread(new Runnable() {
        @Override
        public void run() {
            uploadDatToFirebase(job);
        }
    }).start();

    return true;
}



@Override
public boolean onStopJob(JobParameters job) {
    return false;
}

private void uploadDatToFirebase(final JobParameters parameters) {
    try{
        doSomething();
        Thread.sleep(2000);
    }catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        jobFinished(parameters, true);
    }
}

活动中:

 FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));

    Job myJob = dispatcher.newJobBuilder()
            .setService(InfoJobService.class)
            .setTag("InfoJobService")
            .setRecurring(true)
            .setTrigger(Trigger.executionWindow(30, 60))
            .setLifetime(Lifetime.FOREVER)
            .setReplaceCurrent(false)
            .setConstraints(Constraint.ON_ANY_NETWORK, Constraint.DEVICE_CHARGING)
            .setRetryStrategy(RetryStrategy.DEFAULT_LINEAR)
            .build();

    dispatcher.mustSchedule(myJob);
于 2017-09-23T07:58:57.943 回答