Android jobScheduler will not stop with jobFinished (params, false)

I am trying to create a jobService. This is what onStartJob () looks like.

@Override public boolean onStartJob(JobParameters params) { Log.d(TAG, "onStartJob"); Log.d(TAG, "Params= " + params.getJobId()); param = params; jobFinished(params, false); //startAsync(); return true; } @Override public boolean onStopJob(JobParameters params) { Log.d(TAG, "onStopJob"); return false; } 

Here is the code that should run the task.

 public void startJobScheduler(){ Log.d(TAG, "inside startJobScheduler"); Activity activity = this.cordova.getActivity(); Context context = activity.getApplicationContext(); mJobScheduler = (JobScheduler)context.getSystemService(Context.JOB_SCHEDULER_SERVICE ); JobInfo.Builder job = new JobInfo.Builder(111, new ComponentName(context, JobSchedulerService.class)); job.setPeriodic(60000); Log.d(TAG, "before mJobScheduler.schedule(job.build())"); if( mJobScheduler.schedule( job.build() ) <= 0 ) { Log.d(TAG, "job schedule failed"); } Log.d(TAG, "333"); } 

I can not make him stop. He just shoots every 1-5 minutes. I put jobFinished (params, false) in onStartJob () and commented on the task to try to kill it right after it starts, but it just keeps shooting. It seems that jobFinished () is starting something, since onDestroy () is being called, and my service is being destroyed, but then another job comes up with the same id and starts all its backups.

I have BIND_JOB_SERVICE in the manifest, as shown in each example.

Any ideas on why jobFinished (params, false) doesn't seem to kill setPeriodic (60000)?

+5
source share
2 answers

Well, I realized if anyone else has such a problem.

jobFinished () will not stop the periodic time that you set from continuing. It just says that you are done to release wakelock, so Android should not kill the job.

What I needed to do was recreate the jobScheduler in my service and call cancelAll (). You can also call cancellation (job_id).

 jobScheduler = (JobScheduler)this.getSystemService(Context.JOB_SCHEDULER_SERVICE ); jobScheduler.cancelAll(); 
+5
source

You indicated that the task runs periodically (every 60 seconds), so every 60 ~ seconds a new task is created and executed. jobFinished() job specific and simply indicates that it is running. You have not canceled anything.

Your (currently) accepted answer works to cancel the scheduled task, but if all you want is work done for 60 seconds and then stops, you should omit setPeriodic() and use setOverrideDeadline(60000) instead . The task will be completed within 60 seconds, and after it will no longer be scheduled.

+10
source

All Articles