Java Code Examples for android.app.job.JobScheduler#getPendingJob()

The following examples show how to use android.app.job.JobScheduler#getPendingJob() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BrightnessIdleJob.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example 2
Source File: GcmJobService.java    From ti.goosh with MIT License 6 votes vote down vote up
@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);
	}
}
 
Example 3
Source File: RemoteVerifyJob.java    From Auditor with MIT License 5 votes vote down vote up
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");
    }
}
 
Example 4
Source File: NotificationsJobService.java    From intra42 with Apache License 2.0 5 votes vote down vote up
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());
}
 
Example 5
Source File: ApiHelperForN.java    From cronet with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
/** See {@link JobScheduler#getPendingJob(int)}. */
public static JobInfo getPendingJob(JobScheduler scheduler, int jobId) {
    return scheduler.getPendingJob(jobId);
}