gpt4 book ai didi

android - 在 firebase jobdispatcher 中停止 JobService 内的预定作业

转载 作者:行者123 更新时间:2023-11-29 02:26:30 25 4
gpt4 key购买 nike

我在我的应用程序中使用 firebase jobdispatcher,但我遇到了一个问题。我想在任务完成后停止工作。我尝试在 onStartJob() 方法中调用 selfStop()。但它的 onStopJob() 永远不会被调用。根据我的应用程序中的要求,我正在完成启动工作的 Activity 。那么谁能告诉我如何在 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?"
}

关于android - 在 firebase jobdispatcher 中停止 JobService 内的预定作业,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51910941/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com