下面列出了android.app.job.JobScheduler#getPendingJob ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public static void scheduleJob(Context context) {
JobScheduler jobScheduler = context.getSystemService(JobScheduler.class);
JobInfo pending = jobScheduler.getPendingJob(JOB_ID);
JobInfo jobInfo =
new JobInfo.Builder(JOB_ID, new ComponentName(context, BrightnessIdleJob.class))
.setRequiresDeviceIdle(true)
.setRequiresCharging(true)
.setPeriodic(TimeUnit.HOURS.toMillis(24)).build();
if (pending != null && !pending.equals(jobInfo)) {
jobScheduler.cancel(JOB_ID);
pending = null;
}
if (pending == null) {
jobScheduler.schedule(jobInfo);
}
}
@RequiresApi(api = Build.VERSION_CODES.O)
static void scheduleJob(Context context, Bundle extras) {
ComponentName jobComponentName = new ComponentName(context.getPackageName(), GcmJobService.class.getName());
JobScheduler mJobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
JobInfo existingInfo = mJobScheduler.getPendingJob(JOB_ID);
if (existingInfo != null) {
mJobScheduler.cancel(JOB_ID);
}
JobInfo.Builder jobBuilder = new JobInfo.Builder(JOB_ID, jobComponentName)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).setTransientExtras(extras);
int result = mJobScheduler.schedule(jobBuilder.build());
if (result != JobScheduler.RESULT_SUCCESS) {
Log.e(LCAT, "Could not start job, error code: " + result);
}
}
static void schedule(final Context context, int interval) {
if (interval < MIN_INTERVAL) {
interval = MIN_INTERVAL;
Log.e(TAG, "invalid interval " + interval + " clamped to MIN_INTERVAL " + MIN_INTERVAL);
} else if (interval > MAX_INTERVAL) {
interval = MAX_INTERVAL;
Log.e(TAG, "invalid interval " + interval + " clamped to MAX_INTERVAL " + MAX_INTERVAL);
}
final JobScheduler scheduler = context.getSystemService(JobScheduler.class);
final JobInfo jobInfo = scheduler.getPendingJob(PERIODIC_JOB_ID);
final long intervalMillis = interval * 1000;
final long flexMillis = intervalMillis / 10;
if (jobInfo != null &&
jobInfo.getIntervalMillis() == intervalMillis &&
jobInfo.getFlexMillis() == flexMillis) {
Log.d(TAG, "job already registered");
return;
}
final ComponentName serviceName = new ComponentName(context, RemoteVerifyJob.class);
if (jobInfo == null) {
if (scheduler.schedule(new JobInfo.Builder(FIRST_RUN_JOB_ID, serviceName)
.setOverrideDeadline(intervalMillis - OVERRIDE_OFFSET_MS)
.setPersisted(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build()) == JobScheduler.RESULT_FAILURE) {
throw new RuntimeException("job schedule failed");
}
}
if (scheduler.schedule(new JobInfo.Builder(PERIODIC_JOB_ID, serviceName)
.setPeriodic(intervalMillis, flexMillis)
.setPersisted(true)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
.build()) == JobScheduler.RESULT_FAILURE) {
throw new RuntimeException("job schedule failed");
}
}
public static void schedule(Context context) {
JobScheduler jobScheduler = (JobScheduler) context.getSystemService(Context.JOB_SCHEDULER_SERVICE);
if (jobScheduler == null)
return;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
if (jobScheduler.getPendingJob(JOB_ID) != null)
return;
} else {
List<JobInfo> allPendingJobs = jobScheduler.getAllPendingJobs();
if (!allPendingJobs.isEmpty()) {
for (JobInfo j : allPendingJobs) {
if (j.getId() == JOB_ID)
return;
}
}
}
SharedPreferences settings = AppSettings.getSharedPreferences(context);
int notificationsFrequency = AppSettings.Notifications.getNotificationsFrequency(settings);
ComponentName component = new ComponentName(context, NotificationsJobService.class);
JobInfo.Builder builder = new JobInfo.Builder(JOB_ID, component)
.setPeriodic(60000 * notificationsFrequency);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_NOT_ROAMING);
else
builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY);
jobScheduler.schedule(builder.build());
}
/** See {@link JobScheduler#getPendingJob(int)}. */
public static JobInfo getPendingJob(JobScheduler scheduler, int jobId) {
return scheduler.getPendingJob(jobId);
}