Daniel Braun Daniel Braun - 5 months ago 33
Java Question

Android jobScheduler won't stop with jobFinished(params, false)

I'm tring to create a jobService. Here 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 is supposed to start the job.

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 get it to stop. It just keeps firing every 1-5 mins. I put jobFinished(params, false) in onStartJob() and commented out the task to try to kill it off right after it starts, but it just keeps firing. It seems jobFinished() fires something, as onDestroy() is called and my service gets destroyed, but then another job comes in with the same ID and starts it all back up.

I have BIND_JOB_SERVICE in the manifest like every example shows.

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

Answer

Well I figured it out if anyone else has this problem.

jobFinished() won't stop the periodic time you set from continuing. It just tells the job that you are finished, to release the wakelock, so Android doesn't have to kill the job off forcefully.

What I had to do was recreate the jobScheduler in my service and call cancelAll(). You could also apparently call cancel(job_id).

jobScheduler = (JobScheduler)this.getSystemService(Context.JOB_SCHEDULER_SERVICE );
jobScheduler.cancelAll();
Comments