@Test @Config(minSdk = Build.VERSION_CODES.O) public void getForegroundService_withFlagNoCreate_shouldReturnNullIfRequestCodeIsUnmatched() { Intent intent = new Intent(); PendingIntent.getForegroundService(context, 99, intent, 0); assertThat(PendingIntent.getForegroundService(context, 98, intent, FLAG_NO_CREATE)).isNull(); }
@Test @Config(minSdk = Build.VERSION_CODES.O) public void getForegroundService_withFlagNoCreate_shouldReturnNullIfNoPendingIntentExists() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getForegroundService(context, 99, intent, FLAG_NO_CREATE); assertThat(pendingIntent).isNull(); }
@Test @Config(minSdk = Build.VERSION_CODES.O) public void cancel_shouldRemovePendingIntentForForegroundService() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getForegroundService(context, 99, intent, 100); assertThat(pendingIntent).isNotNull(); pendingIntent.cancel(); assertThat(PendingIntent.getForegroundService(context, 99, intent, FLAG_NO_CREATE)).isNull(); }
@Test @Config(minSdk = Build.VERSION_CODES.O) public void getForegroundService_withFlagNoCreate_shouldReturnExistingIntent() { Intent intent = new Intent(); PendingIntent.getForegroundService(context, 99, intent, 100); Intent identical = new Intent(); PendingIntent saved = PendingIntent.getForegroundService(context, 99, identical, FLAG_NO_CREATE); assertThat(saved).isNotNull(); assertThat(intent).isSameAs(shadowOf(saved).getSavedIntent()); }
@Test @Config(minSdk = Build.VERSION_CODES.O) public void getForegroundService_withNoFlags_shouldReturnExistingIntent() { Intent intent = new Intent(); PendingIntent.getForegroundService(context, 99, intent, 100); Intent identical = new Intent(); PendingIntent saved = PendingIntent.getForegroundService(context, 99, identical, 0); assertThat(saved).isNotNull(); assertThat(intent).isSameAs(shadowOf(saved).getSavedIntent()); }
@Test @Config(minSdk = Build.VERSION_CODES.O) public void getForegroundService_shouldCreateIntentForBroadcast() { Intent intent = new Intent(); PendingIntent pendingIntent = PendingIntent.getForegroundService(context, 99, intent, 100); ShadowPendingIntent shadow = shadowOf(pendingIntent); assertThat(shadow.isActivityIntent()).isFalse(); assertThat(shadow.isBroadcastIntent()).isFalse(); assertThat(shadow.isForegroundServiceIntent()).isTrue(); assertThat(shadow.isServiceIntent()).isFalse(); assertThat(intent).isEqualTo(shadow.getSavedIntent()); assertThat(context).isEqualTo(shadow.getSavedContext()); assertThat(shadow.getRequestCode()).isEqualTo(99); assertThat(shadow.getFlags()).isEqualTo(100); }
public static PendingIntent getForegroundService(Context context, Intent intent) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { return PendingIntent.getForegroundService(context, 0, intent, 0); } else { return PendingIntent.getService(context, 0, intent, 0); } } }
PendingIntent playPausePendingIntent; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { playPausePendingIntent = PendingIntent.getForegroundService(mContext, INTENT_PLAYPAUSE, playPauseIntent, PendingIntent.FLAG_CANCEL_CURRENT); } else { playPausePendingIntent = PendingIntent.getService(mContext, INTENT_PLAYPAUSE, playPauseIntent, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent prevPendingIntent; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { prevPendingIntent = PendingIntent.getForegroundService(mContext, INTENT_PREVIOUS, prevIntent, PendingIntent.FLAG_CANCEL_CURRENT); } else { prevPendingIntent = PendingIntent.getService(mContext, INTENT_PREVIOUS, prevIntent, PendingIntent.FLAG_CANCEL_CURRENT); PendingIntent nextPendingIntent; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { nextPendingIntent = PendingIntent.getForegroundService(mContext, INTENT_NEXT, nextIntent, PendingIntent.FLAG_CANCEL_CURRENT); } else { nextPendingIntent = PendingIntent.getService(mContext, INTENT_NEXT, nextIntent, PendingIntent.FLAG_CANCEL_CURRENT);