我正在使用FirebaseJobDispatcher启动JobService。该服务应在有Internet连接时随时启动。服务启动时,它将与服务器同步未发送的消息。但是我的问题是,当我打开和关闭wifi时,服务无法启动。它只启动一次。
private void scheduleJobForUnsendMessages()
{
System.out.println("JOB SCHEDULED");
Driver driver = new GooglePlayDriver(getApplication());
FirebaseJobDispatcher firebaseJobDispatcher = new FirebaseJobDispatcher(driver);
Job constraintReminderJob = firebaseJobDispatcher.newJobBuilder()
.setService(SynchData.class)
.setTag("tag")
.setConstraints(Constraint.ON_ANY_NETWORK)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(10, 20))
.setRecurring(true)
.setReplaceCurrent(true)
.build();
firebaseJobDispatcher.schedule(constraintReminderJob);
}
这是我的服务方法:
@Override
public boolean onStartJob(JobParameters job)
{
System.out.println("JOB SCHEDULED: STARTED");
if(isOnline(getApplicationContext()))
{
new Thread(new SendInformations()).start();
}
return true;
}
@Override
public boolean onStopJob(JobParameters job)
{
System.out.println("JOB SCHEDULED: STOPPED");
return true;
}