Refine search
public PendingIntent getErrorPendingIntent(int errorCode, Context context, int requestCode) { if (errorCode == ConnectionResult.SUCCESS) { return null; } return PendingIntent.getActivity( context, requestCode, new Intent(), PendingIntent.FLAG_CANCEL_CURRENT); }
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 buildPendingIntent(String action) { Intent i=new Intent(this, getClass()); i.setAction(action); return(PendingIntent.getService(this, 0, i, 0)); } }
static void scheduleAlarms(Context ctxt) { AlarmManager mgr= (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE); Intent i=new Intent(ctxt, PollReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); Intent i2=new Intent(ctxt, EventDemoActivity.class); PendingIntent pi2=PendingIntent.getActivity(ctxt, 0, i2, 0); AlarmManager.AlarmClockInfo ac= new AlarmManager.AlarmClockInfo(System.currentTimeMillis()+PERIOD, pi2); mgr.setAlarmClock(ac, pi); } }
@Test public void testHashCode() { Context ctx = ApplicationProvider.getApplicationContext(); PendingIntent pendingIntent1 = PendingIntent.getActivity(ctx, 99, new Intent("activity"), 100); assertThat(pendingIntent1.hashCode()) .isEqualTo(PendingIntent.getActivity(ctx, 99, new Intent("activity"), 100).hashCode()); assertThat(pendingIntent1.hashCode()) .isNotEqualTo(PendingIntent.getActivity(ctx, 99, new Intent("activity2"), 100).hashCode()); assertThat(pendingIntent1.hashCode()) .isNotEqualTo(PendingIntent.getActivity(ctx, 999, new Intent("activity"), 100).hashCode()); } }
@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 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 send_shouldFillInIntentData() throws Exception { Intent intent = new Intent("action"); Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivity(context, 99, intent, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent fillIntent = new Intent().putExtra("TEST", 23); pendingIntent.send(otherContext, 0, fillIntent); Intent i = shadowOf(otherContext).getNextStartedActivity(); assertThat(i).isNotNull(); assertThat(i.filterEquals(intent)).isTrue(); // Ignore extras. assertThat(i.getIntExtra("TEST", -1)).isEqualTo(23); }
@Test public void set_shouldReplaceAlarmsWithSameIntentReceiver() { alarmManager.set(AlarmManager.ELAPSED_REALTIME, 500, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); alarmManager.set(AlarmManager.ELAPSED_REALTIME, 1000, PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()), 0)); assertThat(shadowAlarmManager.getScheduledAlarms()).hasSize(1); }
@Test public void send_shouldNotUsePreviouslyFilledInLastIntentData() throws Exception { Intent[] intents = {new Intent("first"), new Intent("second")}; Context context = Robolectric.setupActivity(Activity.class); PendingIntent pendingIntent = PendingIntent.getActivities(context, 99, intents, 100); Activity otherContext = Robolectric.setupActivity(Activity.class); Intent firstFillIntent = new Intent(); firstFillIntent.putExtra("KEY1", 23); firstFillIntent.putExtra("KEY2", 24); pendingIntent.send(otherContext, 0, firstFillIntent); ShadowActivity shadowActivity = shadowOf(otherContext); shadowActivity.clearNextStartedActivities(); Intent secondFillIntent = new Intent(); secondFillIntent.putExtra("KEY1", 50); pendingIntent.send(otherContext, 0, secondFillIntent); Intent second = shadowActivity.getNextStartedActivity(); assertThat(second).isNotNull(); assertThat(second.filterEquals(intents[1])).isTrue(); // Ignore extras. assertThat(second.getIntExtra("KEY1", -1)).isEqualTo(50); assertThat(second.hasExtra("KEY2")).isFalse(); }
public static PendingIntent createPendingIntent(Context context, String referenceKey) { setEnabledBlocking(context, DisplayLeakActivity.class, true); Intent intent = new Intent(context, DisplayLeakActivity.class); intent.putExtra(SHOW_LEAK_EXTRA, referenceKey); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); return PendingIntent.getActivity(context, 1, intent, FLAG_UPDATE_CURRENT); }
PendingIntent reply = intent.getParcelableExtra(PENDING_RESULT_EXTRA); InputStream in = null; try { try { URL url = new URL(intent.getStringExtra(URL_EXTRA)); IllustrativeRSS rss = parser.parse(in = url.openStream()); Intent result = new Intent(); result.putExtra(RSS_RESULT_EXTRA, rss); reply.send(this, RESULT_CODE, result); } catch (MalformedURLException exc) { reply.send(INVALID_URL_CODE); } catch (Exception exc) { reply.send(ERROR_CODE);
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 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 scheduleInexactAlarm(Context ctxt, AlarmManager alarms, long period, boolean isDownload) { Intent i=new Intent(ctxt, PollReceiver.class) .putExtra(EXTRA_IS_DOWNLOAD, isDownload); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); alarms.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+period, period, pi); }
private PendingIntent buildStopPendingIntent() { Intent i=new Intent(this, ControlReceiver.class).putExtra(EXTRA_STOP, true); return(PendingIntent.getBroadcast(this, PI_STOP, i, 0)); } }
static void scheduleAlarms(Context ctxt) { AlarmManager mgr= (AlarmManager)ctxt.getSystemService(Context.ALARM_SERVICE); Intent i=new Intent(ctxt, PollReceiver.class); PendingIntent pi=PendingIntent.getBroadcast(ctxt, 0, i, 0); mgr.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + INITIAL_DELAY, PERIOD, pi); } }
/** * Triggers a currently registered {@link AppUsageObserver} with {@code observerId}. * * The observer will be no longer registered afterwards. */ public void triggerRegisteredAppUsageObserver(int observerId, long timeUsedInMillis) { AppUsageObserver observer = appUsageObserversById.remove(observerId); long timeLimitInMillis = observer.timeUnit.toMillis(observer.timeLimit); Intent intent = new Intent() .putExtra(UsageStatsManager.EXTRA_OBSERVER_ID, observerId) .putExtra(UsageStatsManager.EXTRA_TIME_LIMIT, timeLimitInMillis) .putExtra(UsageStatsManager.EXTRA_TIME_USED, timeUsedInMillis); try { observer.callbackIntent.send(RuntimeEnvironment.application, 0, intent); } catch (CanceledException e) { throw new RuntimeException(e); } }
public void notifyMe(View v) { Intent i=new Intent(this, AlarmReceiver.class) .putExtra(AlarmReceiver.EXTRA_TYPE, type.getSelectedItemPosition()); PendingIntent pi=PendingIntent.getBroadcast(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager mgr=(AlarmManager)getSystemService(ALARM_SERVICE); mgr.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime()+(1000*delay.getProgress()), pi); } }
@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); }