【发布时间】:2019-01-25 09:56:47
【问题描述】:
我在我的应用程序中使用 firebase jobdispatcher,但我遇到了一个问题。我想在任务完成后停止工作。我尝试在onStartJob() 方法中调用selfStop()。但它的onStopJob() 永远不会被调用。根据我的应用程序中的要求,我正在完成开始工作的活动。那么谁能告诉我如何在JobService 类中停止工作。
代码示例:
// 使用 Google Play 驱动程序创建一个新的调度程序。
FirebaseJobDispatcher dispatcher = new FirebaseJobDispatcher(new GooglePlayDriver(this));
Job downtimeOverNotificationJob = dispatcher.newJobBuilder()
.setService(AppJobService.class) // the JobService that will be called
.setTag("my-unique-tag") // uniquely identifies the job
.setTrigger(Trigger.executionWindow(20,20))
.build();
dispatcher.mustSchedule(downtimeOverNotificationJob);
@Override
public boolean onStartJob(JobParameters job) {
Toast.makeText(this, "Job started", Toast.LENGTH_SHORT).show();
new Handler(Looper.getMainLooper()).postDelayed(() -> {
//Do something after 10000ms
stopSelf();
}, 5000);
return false; // Answers the question: "Is there still work going on?"
}
@Override
public boolean onStopJob(JobParameters job) {
Vibrator v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
// Vibrate for 500 milliseconds
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500,VibrationEffect.DEFAULT_AMPLITUDE));
}else{
//deprecated in API 26
v.vibrate(500);
}
return false; // Answers the question: "Should this job be retried?"
}
感谢任何帮助。
【问题讨论】:
标签: android firebase-job-dispatcher