在Firebase JobDispatcher中的JobService中停止计划的作业

时间:2018-08-18 17:11:05

标签: android firebase-job-dispatcher

我在我的应用程序中使用firebase jobdispatcher,但遇到了问题。我想在任务完成后停止工作。我尝试在Callable<T>方法内调用selfStop()。但是它的onStartJob()从未被调用。按照我的应用程序中的要求,我正在完成启动工作的活动。谁能告诉我如何在onStopJob()类内停止工作。

代码示例:

//使用Google Play驱动程序创建新的调度程序。

JobService

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

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?"
        }