I am trying to use JobScheduler
to schedule a job per 3 seconds.
I have made my Job.Builder
ready, but still when I schedule the job, onStartJob()
is not getting called.
I am not sure what I am missing please help me.
JobBuilder
:
ComponentName componentName = new ComponentName(this,JobSchedulerService.class);
JobInfo.Builder builder = new JobInfo.Builder(0,componentName)
.setPeriodic(5 * 1000);
JobScheduler jobScheduler = (JobScheduler) getBaseContext().getSystemService(Context.JOB_SCHEDULER_SERVICE);
int id = jobScheduler.schedule(builder.build());
Log.i("Service","ID is :"+id);
Inside JobService
,
public class JobSchedulerService extends JobService {
@Override
public boolean onStartJob(JobParameters jobParameters) {
Message message = new Message();
message.what = Utils.JOB_SCHEDULER_WHAT;
message.obj = jobParameters;
handler.sendMessage(message);
return true;
}
@Override
public boolean onStopJob(JobParameters jobParameters) {
return false;
}
private final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
JobParameters jobParameters = (JobParameters)msg.obj;
Log.i("Service:","ServiceID:"+jobParameters.getJobId());
}
};
}
Please let me know how to get this done.
Job scheduler has a minimum of setPeridic value of 15 minutes, so if you pass in less than 15 minutes (like in your case), jobScheduler just clamps up your periodic value to 15 minutes. Consider waiting 15 minutes or use setOverrideDeadline(long). see https://catinean.com/2014/10/19/smart-background-tasks-with-jobscheduler/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With