Остановить запланированное задание внутри JobService в firebase jobdispatcher

Я использую 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?"
    }

Любая помощь приветствуется.

1 ответ

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?"
        }
Другие вопросы по тегам