我在应用中使用了Firebase jobdispatcher,但遇到了一个问题。 我想在任务完成后停止作业。 我尝试在方法中调用selfStop()
onStartJob()
。但它的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?"
}
任何帮助,不胜感激。
Need to call jobFinished(job, false) if you want to restart job manually and get onStartJob(JobParameters job) callback.
Example:
@Override
public boolean onStartJob(JobParameters job) {
Toast.makeText(this, "Job started", Toast.LENGTH_SHORT).show();
new Handler(Looper.getMainLooper()).postDelayed(() -> {
//Do something after 10000ms
jobFinished(job, false);
}, 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?"
}