【问题标题】:Stop scheduled job inside JobService in firebase jobdispatcher在firebase jobdispatcher中停止JobService内的计划作业
【发布时间】: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


    【解决方案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?"
            }
    

    【讨论】:

    • 看不懂,能否给一段代码。
    • 它不调用 onStopJob() ,我试过这个。我怎么知道工作是被解雇还是停止。
    • jobScheduler = (JobScheduler)this.getSystemService(Context.JOB_SCHEDULER_SERVICE);它为您提供所有工作\
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多