最佳答案
我认为这不是使用JobScheduler的最佳用例,但你可以!首先你应该创造
public class ExerciseJobService extends JobService {
private Context context;
public ExerciseJobService(Context context) {
this.context = context
}
@Override
public boolean onStartJob(JobParameters params) {
Intent intent = new Intent(context,YOUR_SERVICE.class)
startForegroundService(intent)
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
return true;
}
}
之后安排工作
private FirebaseJobDispatcher jobDispatcher = FirebaseJobDispatcher(GooglePlayDriver(context))
Job myJob = jobDispatcher.newJobBuilder()
.setService(ExerciseJobService::class.java)
.setTag("SIMPLE_TAG")
.setTrigger(Trigger.NOW)
.build()
jobDispatcher.mustSchedule(myJob)