我想得到一个一直重复的任务。对于android <21可以正常工作,但是在我的手机处于锁定状态3小时后,在andorid> 21上无法正常工作。当我解锁手机时,任务正在运行
这是您的任务:
public class JobDispacherService extends JobService {
private Preferences prefs = null;
private UplaudPossitionTask uplaudPossitionTask;
@Override
public boolean onStartJob(JobParameters job) {
// Do some work here
uplaudPossitionTask = new UplaudPossitionTask() {
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
jobFinished(job, false);
}
};
uplaudPossitionTask.execute();
return false; // Answers the question: "Is there still work going on?"
}
@Override
public boolean onStopJob(JobParameters job) {
return true; // Answers the question: "Should this job be retried?"
}
private class UplaudPossitionTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
uploadPosition();
return null;
}
}
}
我就这样叫这项服务:
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(context));
Job myJob = dispatcher.newJobBuilder()
.setService(JobDispacherService.class)
.setTag("my-unique-tag")
.setRecurring(true)
.setLifetime(Lifetime.FOREVER)
.setTrigger(Trigger.executionWindow(10, (int) 15))
.setReplaceCurrent(false)
.setRetryStrategy(RetryStrategy.DEFAULT_EXPONENTIAL)
.setConstraints(Constraint.ON_ANY_NETWORK)
.build();
dispatcher.mustSchedule(myJob);