private static final int PERIODIC_JOB_ID = 1;
public static void setPeriodicSchedule(Context context) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo jobInfo = new JobInfo.Builder(PERIODIC_JOB_ID,
new ComponentName(context, MyJobService.class))
.setPeriodic(TimeUnit.MINUTES.toMillis(30))
.setPersisted(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build();
scheduler.schedule(jobInfo);
}
public static void cancelPeriodicSchedule(Context context) {
JobScheduler scheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
scheduler.cancel(PERIODIC_JOB_ID);
} }