private static PendingIntent createBroadcastIntent( String action, Context context, int instanceId) { Intent intent = new Intent(action).setPackage(context.getPackageName()); intent.putExtra(EXTRA_INSTANCE_ID, instanceId); return PendingIntent.getBroadcast( context, instanceId, intent, PendingIntent.FLAG_CANCEL_CURRENT); }
private PendingIntent buildCancelContent(Context context, int id, String url) { Intent intentCancel = new Intent(context, NotificationCancelReceiver.class); intentCancel.setAction(NotificationCancelReceiver.ACTION); intentCancel.putExtra("TAG", url); PendingIntent pendingIntentCancel = PendingIntent.getBroadcast(context, id << 3, intentCancel, PendingIntent.FLAG_UPDATE_CURRENT); LogUtils.i(TAG, "id<<3:" + (id << 3)); return pendingIntentCancel; }
static void cancelAlarm(Context ctxt, AlarmManager alarms) { Intent i=new Intent(ctxt, PollReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); alarms.cancel(pi); } }
static void scheduleExactAlarm(Context ctxt, AlarmManager alarms, long period, boolean isDownload) { Intent i=new Intent(ctxt, PollReceiver.class) .putExtra(EXTRA_PERIOD, period) .putExtra(EXTRA_IS_DOWNLOAD, isDownload); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); alarms.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+period, pi); }
static void cancelAlarm(Context ctxt, AlarmManager alarms) { Intent i=new Intent(ctxt, PollReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); alarms.cancel(pi); } }
protected PendingIntent getPendingIntent(int jobId, boolean exact, @Nullable Bundle transientExtras, int flags) { Intent intent = PlatformAlarmReceiver.createIntent(mContext, jobId, exact, transientExtras); // repeating PendingIntent with service seams to have problems try { return PendingIntent.getBroadcast(mContext, jobId, intent, flags); } catch (Exception e) { // java.lang.SecurityException: Permission Denial: getIntentSender() from pid=31482, uid=10057, // (need uid=-1) is not allowed to send as package com.evernote mCat.e(e); return null; } }
@Test public void getBroadcast_withFlagNoCreate_shouldReturnNullIfRequestCodeIsUnmatched() { Intent intent = new Intent(); PendingIntent.getBroadcast(context, 99, intent, 0); assertThat(PendingIntent.getBroadcast(context, 98, intent, FLAG_NO_CREATE)).isNull(); }
@Test public void getBroadcast_withFlagNoCreate_shouldReturnNullIfNoPendingIntentExists() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 99, intent, FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test public void updatePendingIntent() { Intent intent = new Intent().putExtra("whatever", 5); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0); ShadowPendingIntent shadowPendingIntent = shadowOf(pendingIntent); // Absent FLAG_UPDATE_CURRENT, this should fail to update the Intent extra. intent = new Intent().putExtra("whatever", 77); PendingIntent.getBroadcast(context, 0, intent, 0); assertThat(shadowPendingIntent.getSavedIntent().getIntExtra("whatever", -1)).isEqualTo(5); // With FLAG_UPDATE_CURRENT, this should succeed in updating the Intent extra. PendingIntent.getBroadcast(context, 0, intent, FLAG_UPDATE_CURRENT); assertThat(shadowPendingIntent.getSavedIntent().getIntExtra("whatever", -1)).isEqualTo(77); }
@Test public void cancel_shouldRemovePendingIntentForBroadcast() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 99, intent, 100); assertThat(pendingIntent).isNotNull(); pendingIntent.cancel(); assertThat(PendingIntent.getBroadcast(context, 99, intent, FLAG_NO_CREATE)).isNull(); }
@Test public void getBroadcast_withNoFlags_shouldReturnExistingIntent() { Intent intent = new Intent(); PendingIntent.getBroadcast(context, 99, intent, 100); Intent identical = new Intent(); PendingIntent saved = PendingIntent.getBroadcast(context, 99, identical, 0); assertThat(saved).isNotNull(); assertThat(intent).isSameAs(shadowOf(saved).getSavedIntent()); }
@Test public void getBroadcast_withFlagNoCreate_shouldReturnExistingIntent() { Intent intent = new Intent(); PendingIntent.getBroadcast(context, 99, intent, 100); Intent identical = new Intent(); PendingIntent saved = PendingIntent.getBroadcast(context, 99, identical, FLAG_NO_CREATE); assertThat(saved).isNotNull(); assertThat(intent).isSameAs(shadowOf(saved).getSavedIntent()); }
@Test public void cancel_removesMatchingPendingIntentsWithActions() { Intent newIntent = new Intent("someAction"); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, newIntent, 0); alarmManager.set(AlarmManager.RTC, 1337, pendingIntent); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent("anotherAction"), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); alarmManager.cancel(PendingIntent.getBroadcast(context, 0, new Intent("someAction"), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(0); }
@Test @Config(minSdk = Build.VERSION_CODES.P) public void testRegisterAppUsageObserver_duplicateObserverIds_shouldOverrideExistingObserver() { PendingIntent pendingIntent1 = PendingIntent.getBroadcast(context, 0, new Intent("ACTION1"), 0); usageStatsManager.registerAppUsageObserver( 12, new String[] {"com.package1", "com.package2"}, 123L, TimeUnit.MINUTES, pendingIntent1); PendingIntent pendingIntent2 = PendingIntent.getBroadcast(context, 0, new Intent("ACTION2"), 0); usageStatsManager.registerAppUsageObserver( 12, new String[] {"com.package3"}, 456L, TimeUnit.SECONDS, pendingIntent2); assertThat(shadowOf(usageStatsManager).getRegisteredAppUsageObservers()) .containsExactly( new AppUsageObserver( 12, ImmutableList.of("com.package3"), 456L, TimeUnit.SECONDS, pendingIntent2)); }
@Test public void startIntentSender_broadcastIntent() throws IntentSender.SendIntentException { PendingIntent intent = PendingIntent.getBroadcast( context, 0, new Intent().setClassName(context, "BroadcastIntent"), PendingIntent.FLAG_UPDATE_CURRENT); context.startIntentSender(intent.getIntentSender(), null, 0, 0, 0); assertThat(shadowOf(context).getBroadcastIntents().get(0).getComponent().getClassName()) .isEqualTo("BroadcastIntent"); }
@Test @Config(minSdk = JELLY_BEAN_MR2) public void build_addsActionToNotification() throws Exception { PendingIntent action = PendingIntent.getBroadcast(ApplicationProvider.getApplicationContext(), 0, null, 0); Notification notification = builder.addAction(0, "Action", action).build(); assertThat(notification.actions[0].actionIntent).isEqualTo(action); }
@Test public void shouldRegisterLocationUpdatesWhenProviderGiven() throws Exception { shadowLocationManager.setProviderEnabled(GPS_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.GPS_PROVIDER, true); Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); locationManager.requestLocationUpdates(GPS_PROVIDER, 0, 0, someLocationListenerPendingIntent); assertThat(shadowLocationManager.getRequestLocationUdpateProviderPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(GPS_PROVIDER); }
@Test public void shouldThrowExceptionWhenRequestingLocationUpdatesAndNoProviderIsFound() throws Exception { Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_FINE); try { shadowLocationManager.requestLocationUpdates(0, 0, criteria, someLocationListenerPendingIntent); Assert.fail("When requesting location updates the intent must not be null!"); } catch (Exception e) { // No worries, everything is fine... } }
@Test public void getBroadcast_shouldCreateIntentForBroadcast() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 99, intent, 100); ShadowPendingIntent shadow = shadowOf(pendingIntent); assertThat(shadow.isActivityIntent()).isFalse(); assertThat(shadow.isBroadcastIntent()).isTrue(); assertThat(shadow.isServiceIntent()).isFalse(); assertThat(shadow.isForegroundServiceIntent()).isFalse(); assertThat(intent).isEqualTo(shadow.getSavedIntent()); assertThat(context).isEqualTo(shadow.getSavedContext()); assertThat(shadow.getRequestCode()).isEqualTo(99); assertThat(shadow.getFlags()).isEqualTo(100); }
@Test public void shouldRegisterLocationUpdatesWhenCriteriaGiven() throws Exception { shadowLocationManager.setProviderEnabled(NETWORK_PROVIDER, true); shadowLocationManager.setBestProvider(LocationManager.NETWORK_PROVIDER, true); Criteria criteria = new Criteria(); criteria.setAccuracy(Criteria.ACCURACY_COARSE); Intent someIntent = new Intent("some_action"); PendingIntent someLocationListenerPendingIntent = PendingIntent.getBroadcast(context, 0, someIntent, PendingIntent.FLAG_UPDATE_CURRENT); Criteria someCriteria = new Criteria(); someCriteria.setAccuracy(Criteria.ACCURACY_COARSE); locationManager.requestLocationUpdates(0, 0, someCriteria, someLocationListenerPendingIntent); assertThat(shadowLocationManager.getRequestLocationUdpateCriteriaPendingIntents().get(someLocationListenerPendingIntent)).isEqualTo(someCriteria); }