@Test public void verifyAlarmIsCanceledAfterStart() throws Exception { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); JobConfig.forceApi(JobApi.V_21); int jobId = scheduleJob(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); boolean started = TransientBundleCompat.startWithTransientBundle(context(), mJobManagerRule.getManager().getJobRequest(jobId)); assertThat(started).isTrue(); pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test public void verifyAlarmNotCanceledForPeriodicAfterStart() throws Exception { assumeTrue(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP); JobConfig.forceApi(JobApi.V_21); Bundle extras = new Bundle(); extras.putString("key", "value"); int jobId = new JobRequest.Builder("tag") .setPeriodic(TimeUnit.DAYS.toMillis(1)) .setTransientExtras(extras) .build() .schedule(); assertThat(mJobManagerRule.getAllPendingJobsFromScheduler()).isNotNull().isNotEmpty(); JobRequest request = mJobManagerRule.getManager().getJobRequest(jobId); assertThat(request.isTransient()).isTrue(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); boolean started = TransientBundleCompat.startWithTransientBundle(context(), request); assertThat(started).isTrue(); pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNotNull(); }
@Override public void run() { try { final int jobId = params.getJobId(); final JobProxy.Common common = new JobProxy.Common(PlatformJobService.this, CAT, jobId); // don't mark starting! final JobRequest request = common.getPendingRequest(true, false); if (request == null) { return; } if (request.isTransient()) { if (TransientBundleCompat.startWithTransientBundle(PlatformJobService.this, request)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // should only happen during testing if an API is disabled CAT.d("PendingIntent for transient bundle is not null although running on O, using compat mode, request %s", request); } return; } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { CAT.d("PendingIntent for transient job %s expired", request); return; } } common.markStarting(request); common.executeJobRequest(request, getTransientBundle(params)); } finally { // do not reschedule jobFinished(params, false); } } });
@Override public void run() { try { final int jobId = params.getJobId(); final JobProxy.Common common = new JobProxy.Common(PlatformJobService.this, CAT, jobId); // don't mark starting! final JobRequest request = common.getPendingRequest(true, false); if (request == null) { return; } if (request.isTransient()) { if (TransientBundleCompat.startWithTransientBundle(PlatformJobService.this, request)) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // should only happen during testing if an API is disabled CAT.d("PendingIntent for transient bundle is not null although running on O, using compat mode, request %s", request); } return; } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) { CAT.d("PendingIntent for transient job %s expired", request); return; } } common.markStarting(request); common.executeJobRequest(request, getTransientBundle(params)); } finally { // do not reschedule jobFinished(params, false); } } });