Here are the examples of the java api class android.app.job.JobInfo taken from open source projects.
1. ConnectivityConstraintTestActivity#testConnectivityConstraintFailsImpl()
Project: CtsVerifier
File: ConnectivityConstraintTestActivity.java
File: ConnectivityConstraintTestActivity.java
private void testConnectivityConstraintFailsImpl(int requiredNetworkType, int jobId) { // Use arguments provided to construct job with required connectivity constraint. JobInfo testJob = new JobInfo.Builder(jobId, mMockComponent).setRequiredNetworkType(requiredNetworkType).build(); mTestEnvironment.setUp(); mTestEnvironment.setExpectedExecutions(0); mJobScheduler.schedule(testJob); // Send intent to kick off ready jobs that the JobScheduler might be lazily holding on to. sendBroadcastAndBlockForResult(EXPEDITE_STABLE_CHARGING); boolean testPassed; try { testPassed = mTestEnvironment.awaitTimeout(); } catch (InterruptedException e) { testPassed = false; } runOnUiThread(new ConnectivityConstraintTestResultRunner(jobId, testPassed)); }
2. ConnectivityConstraintTestActivity#testNoConnectivityConstraintExecutes_noConnectivity()
Project: CtsVerifier
File: ConnectivityConstraintTestActivity.java
File: ConnectivityConstraintTestActivity.java
private void testNoConnectivityConstraintExecutes_noConnectivity() { JobInfo testJob = new JobInfo.Builder(NO_CONNECTIVITY_JOB_ID, mMockComponent).setRequiredNetworkType(JobInfo.NETWORK_TYPE_NONE).setOverrideDeadline(// Will not expire. 100000L).build(); mTestEnvironment.setUp(); mTestEnvironment.setExpectedExecutions(1); mJobScheduler.schedule(testJob); // Send intent to kick off ready jobs that the JobScheduler might be lazily holding on to. sendBroadcastAndBlockForResult(EXPEDITE_STABLE_CHARGING); boolean testPassed; try { testPassed = mTestEnvironment.awaitExecution(); } catch (InterruptedException e) { testPassed = false; } runOnUiThread(new ConnectivityConstraintTestResultRunner(NO_CONNECTIVITY_JOB_ID, testPassed)); }
3. ChargingConstraintTestActivity#testChargingConstraintExecutes_onCharging()
Project: CtsVerifier
File: ChargingConstraintTestActivity.java
File: ChargingConstraintTestActivity.java
/** * Test blocks and can't be run on the main thread. */ private void testChargingConstraintExecutes_onCharging() { mTestEnvironment.setUp(); JobInfo delayConstraintAndUnexpiredDeadline = new JobInfo.Builder(ON_CHARGING_JOB_ID, mMockComponent).setRequiresCharging(true).build(); mTestEnvironment.setExpectedExecutions(1); mJobScheduler.schedule(delayConstraintAndUnexpiredDeadline); // Force the JobScheduler to consider any jobs that have charging constraints. sendBroadcast(EXPEDITE_STABLE_CHARGING); boolean testPassed; try { testPassed = mTestEnvironment.awaitExecution(); } catch (InterruptedException e) { testPassed = false; } runOnUiThread(new ChargingConstraintTestResultRunner(ON_CHARGING_JOB_ID, testPassed)); }
4. ChargingConstraintTestActivity#testChargingConstraintFails_notCharging()
Project: CtsVerifier
File: ChargingConstraintTestActivity.java
File: ChargingConstraintTestActivity.java
/** * Test blocks and can't be run on the main thread. */ private void testChargingConstraintFails_notCharging() { mTestEnvironment.setUp(); mTestEnvironment.setExpectedExecutions(0); JobInfo runOnCharge = new JobInfo.Builder(OFF_CHARGING_JOB_ID, mMockComponent).setRequiresCharging(true).build(); mJobScheduler.schedule(runOnCharge); // Send intent to kick off any jobs. This will be a no-op as the device is not plugged in; // the JobScheduler tracks charging state independently. sendBroadcastAndBlockForResult(EXPEDITE_STABLE_CHARGING); boolean testPassed; try { testPassed = mTestEnvironment.awaitTimeout(); } catch (InterruptedException e) { testPassed = false; } mDeviceUnpluggedTestPassed = testPassed; runOnUiThread(new ChargingConstraintTestResultRunner(OFF_CHARGING_JOB_ID, testPassed)); }
5. CBWatcherService#bindJobScheduler()
Project: Clip-Stack
File: CBWatcherService.java
File: CBWatcherService.java
@TargetApi(Build.VERSION_CODES.LOLLIPOP) private void bindJobScheduler() { // JobScheduler for auto clean sqlite JobInfo job = new JobInfo.Builder(JOB_ID, new ComponentName(this, SyncJobService.class)).setRequiresCharging(true).setRequiresDeviceIdle(true).setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED).setPeriodic(24 * 60 * 60 * 1000).setPersisted(true).build(); JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); jobScheduler.cancel(JOB_ID); jobScheduler.schedule(job); }
6. SyncUtils#requestSync()
Project: androidtv-sample-inputs
File: SyncUtils.java
File: SyncUtils.java
public static void requestSync(Context context, String inputId, boolean currentProgramOnly) { PersistableBundle pBundle = new PersistableBundle(); pBundle.putBoolean(ContentResolver.SYNC_EXTRAS_MANUAL, true); pBundle.putBoolean(ContentResolver.SYNC_EXTRAS_EXPEDITED, true); pBundle.putString(SyncJobService.BUNDLE_KEY_INPUT_ID, inputId); pBundle.putBoolean(SyncJobService.BUNDLE_KEY_CURRENT_PROGRAM_ONLY, currentProgramOnly); JobInfo.Builder builder = new JobInfo.Builder(REQUEST_SYNC_JOB_ID, new ComponentName(context, SyncJobService.class)); JobInfo jobInfo = builder.setExtras(pBundle).setOverrideDeadline(SyncJobService.OVERRIDE_DEADLINE_MILLIS).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build(); scheduleJob(context, jobInfo); Intent intent = new Intent(SyncJobService.ACTION_SYNC_STATUS_CHANGED); intent.putExtra(SyncJobService.BUNDLE_KEY_INPUT_ID, inputId); intent.putExtra(SyncJobService.SYNC_STATUS, SyncJobService.SYNC_STARTED); LocalBroadcastManager.getInstance(context).sendBroadcast(intent); }
7. SyncUtils#setUpPeriodicSync()
Project: androidtv-sample-inputs
File: SyncUtils.java
File: SyncUtils.java
public static void setUpPeriodicSync(Context context, String inputId) { PersistableBundle pBundle = new PersistableBundle(); pBundle.putString(SyncJobService.BUNDLE_KEY_INPUT_ID, inputId); JobInfo.Builder builder = new JobInfo.Builder(PERIODIC_SYNC_JOB_ID, new ComponentName(context, SyncJobService.class)); JobInfo jobInfo = builder.setExtras(pBundle).setPeriodic(SyncJobService.FULL_SYNC_FREQUENCY_MILLIS).setPersisted(true).setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY).build(); scheduleJob(context, jobInfo); }
8. JobProxy21#isPlatformJobScheduled()
Project: android-job
File: JobProxy21.java
File: JobProxy21.java
@Override public boolean isPlatformJobScheduled(JobRequest request) { List<JobInfo> pendingJobs; try { pendingJobs = getJobScheduler().getAllPendingJobs(); } catch (Exception e) { CAT.e(e); return false; } if (pendingJobs == null || pendingJobs.isEmpty()) { return false; } int requestId = request.getJobId(); for (JobInfo info : pendingJobs) { if (info.getId() == requestId) { return true; } } return false; }
9. JobProxy21#plantPeriodic()
Project: android-job
File: JobProxy21.java
File: JobProxy21.java
@Override public void plantPeriodic(JobRequest request) { JobInfo jobInfo = createBaseBuilder(request).setPeriodic(request.getIntervalMs()).setRequiresCharging(request.requiresCharging()).setRequiresDeviceIdle(request.requiresDeviceIdle()).setRequiredNetworkType(convertNetworkType(request.requiredNetworkType())).setPersisted(request.isPersisted()).build(); int scheduleResult; try { scheduleResult = getJobScheduler().schedule(jobInfo); } catch (Exception e) { CAT.e(e); scheduleResult = JobScheduler.RESULT_FAILURE; } CAT.d("Schedule periodic jobInfo %s, %s, interval %s", scheduleResult == JobScheduler.RESULT_SUCCESS ? "success" : "failure", request, JobUtil.timeToString(request.getIntervalMs())); }
10. JobProxy21#plantOneOff()
Project: android-job
File: JobProxy21.java
File: JobProxy21.java
@Override public void plantOneOff(JobRequest request) { JobInfo jobInfo = createBaseBuilder(request).setMinimumLatency(Common.getStartMs(request)).setOverrideDeadline(Common.getEndMs(request)).setRequiresCharging(request.requiresCharging()).setRequiresDeviceIdle(request.requiresDeviceIdle()).setRequiredNetworkType(convertNetworkType(request.requiredNetworkType())).setPersisted(request.isPersisted()).build(); int scheduleResult; try { scheduleResult = getJobScheduler().schedule(jobInfo); } catch (Exception e) { CAT.e(e); scheduleResult = JobScheduler.RESULT_FAILURE; } CAT.d("Schedule one-off jobInfo %s, %s, start %s, end %s", scheduleResult == JobScheduler.RESULT_SUCCESS ? "success" : "failure", request, JobUtil.timeToString(Common.getStartMs(request)), JobUtil.timeToString(Common.getEndMs(request))); }