private final PendingIntent retrievePlaybackAction(final String action) { final ComponentName serviceName = new ComponentName(this, MusicService.class); Intent intent = new Intent(action); intent.setComponent(serviceName); return PendingIntent.getService(this, 0, intent, 0); }
private final PendingIntent retrievePlaybackAction(final String action) { final ComponentName serviceName = new ComponentName(this, MediaService.class); Intent intent = new Intent(action); intent.setComponent(serviceName); return PendingIntent.getService(this, 0, intent, 0); }
private PendingIntent buildPendingIntent(String action) { Intent i=new Intent(this, getClass()); i.setAction(action); return(PendingIntent.getService(this, 0, i, 0)); }
private PendingIntent buildPendingIntent(String action) { Intent i=new Intent(this, getClass()); i.setAction(action); return(PendingIntent.getService(this, 0, i, 0)); } }
@Override public PendingIntent createMarkMessageAsReadPendingIntent(MessageReference messageReference, int notificationId) { Intent intent = NotificationActionService.createMarkMessageAsReadIntent(context, messageReference); return PendingIntent.getService(context, notificationId, intent, PendingIntent.FLAG_UPDATE_CURRENT); }
public static void persistBundle(@NonNull Context context, @NonNull JobRequest request) { Intent intent = PlatformAlarmServiceExact.createIntent(context, request.getJobId(), request.getTransientExtras()); PendingIntent pendingIntent = PendingIntent.getService(context, request.getJobId(), intent, PendingIntent.FLAG_UPDATE_CURRENT); long when = System.currentTimeMillis() + TimeUnit.DAYS.toMillis(1000); AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); alarmManager.setExact(AlarmManager.RTC, when, pendingIntent); }
@Test public void getService_withFlagNoCreate_shouldReturnNullIfRequestCodeIsUnmatched() { Intent intent = new Intent(); PendingIntent.getService(context, 99, intent, 0); assertThat(PendingIntent.getService(context, 98, intent, FLAG_NO_CREATE)).isNull(); }
@Test public void immutableFlag_differentiatesPendingIntents() { Intent intent = new Intent(); PendingIntent immutable = PendingIntent.getService(context, 0, intent, FLAG_IMMUTABLE); PendingIntent notImmutable = PendingIntent.getService(context, 0, intent, FLAG_UPDATE_CURRENT); assertThat(immutable).isNotSameAs(notImmutable); }
@Test public void oneShotFlag_differentiatesPendingIntents() { Intent intent = new Intent(); PendingIntent oneShot = PendingIntent.getService(context, 0, intent, FLAG_ONE_SHOT); PendingIntent notOneShot = PendingIntent.getService(context, 0, intent, FLAG_UPDATE_CURRENT); assertThat(oneShot).isNotSameAs(notOneShot); }
@Test public void getService_withFlagNoCreate_shouldReturnNullIfNoPendingIntentExists() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getService(context, 99, intent, FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test public void cancel_shouldRemovePendingIntentForService() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getService(context, 99, intent, 100); assertThat(pendingIntent).isNotNull(); pendingIntent.cancel(); assertThat(PendingIntent.getService(context, 99, intent, FLAG_NO_CREATE)).isNull(); }
@Test public void getService_withNoFlags_shouldReturnExistingIntent() { Intent intent = new Intent(); PendingIntent.getService(context, 99, intent, 100); Intent identical = new Intent(); PendingIntent saved = PendingIntent.getService(context, 99, identical, 0); assertThat(saved).isNotNull(); assertThat(intent).isSameAs(shadowOf(saved).getSavedIntent()); }
@Test public void getService_withFlagNoCreate_shouldReturnExistingIntent() { Intent intent = new Intent(); PendingIntent.getService(context, 99, intent, 100); Intent identical = new Intent(); PendingIntent saved = PendingIntent.getService(context, 99, identical, FLAG_NO_CREATE); assertThat(saved).isNotNull(); assertThat(intent).isSameAs(shadowOf(saved).getSavedIntent()); }
@Test public void schedule_useRequestCodeToMatchExistingPendingIntents() throws Exception { Intent intent = new Intent("ACTION!"); PendingIntent pI = PendingIntent.getService(context, 1, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI); PendingIntent pI2 = PendingIntent.getService(context, 2, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI2); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(2); }
@Test public void send_oneShotPendingIntent_shouldCancel() throws CanceledException { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, FLAG_ONE_SHOT); assertThat(shadowOf(pendingIntent).isCanceled()).isFalse(); pendingIntent.send(); assertThat(shadowOf(pendingIntent).isCanceled()).isTrue(); assertThat(PendingIntent.getService(context, 0, intent, FLAG_ONE_SHOT | FLAG_NO_CREATE)) .isNull(); }
@Test public void startIntentSender_serviceIntent() throws IntentSender.SendIntentException { PendingIntent intent = PendingIntent.getService( context, 0, new Intent().setClassName(context, "ServiceIntent"), PendingIntent.FLAG_UPDATE_CURRENT); context.startIntentSender(intent.getIntentSender(), null, 0, 0, 0); assertThat(shadowOf(context).getNextStartedService().getComponent().getClassName()) .isEqualTo("ServiceIntent"); }
@Test public void cancel_useRequestCodeToMatchExistingPendingIntents() throws Exception { Intent intent = new Intent("ACTION!"); PendingIntent pI = PendingIntent.getService(context, 1, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI); PendingIntent pI2 = PendingIntent.getService(context, 2, intent, 0); alarmManager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, 10, pI2); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(2); alarmManager.cancel(pI); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); assertThat(shadowAlarmManager.getNextScheduledAlarm().operation).isEqualTo(pI2); }
@Test public void verifyAlarmIsCanceled() 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(); mJobManagerRule.getManager().cancel(jobId); pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test public void getService_shouldCreateIntentForBroadcast() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getService(context, 99, intent, 100); ShadowPendingIntent shadow = shadowOf(pendingIntent); assertThat(shadow.isActivityIntent()).isFalse(); assertThat(shadow.isBroadcastIntent()).isFalse(); assertThat(shadow.isForegroundServiceIntent()).isFalse(); assertThat(shadow.isServiceIntent()).isTrue(); assertThat(intent).isEqualTo(shadow.getSavedIntent()); assertThat(context).isEqualTo(shadow.getSavedContext()); assertThat(shadow.getRequestCode()).isEqualTo(99); assertThat(shadow.getFlags()).isEqualTo(100); }
@Test public void verifyNativeImplementationIsUsedWithO() throws Exception { // ignore test if not supported assumeTrue(JobApi.V_26.isSupported(InstrumentationRegistry.getTargetContext())); JobConfig.forceApi(JobApi.V_26); int jobId = scheduleJob(); final Intent intent = PlatformAlarmServiceExact.createIntent(context(), jobId, null); PendingIntent pendingIntent = PendingIntent.getService(context(), jobId, intent, PendingIntent.FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }