Quoting Android developer docs
AlarmManager alarmManager =
(AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
PendingIntent pendingIntent =
PendingIntent.getService(context, requestId, intent,
PendingIntent.FLAG_NO_CREATE);
if (pendingIntent != null && alarmManager != null) {
alarmManager.cancel(pendingIntent);
}
should be able to retrieve a pendingIntent using it’s requestCode and then cancel it, However, when I try to retrieve it using
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(MainActivity.this, AlarmBroadcastReciever.class);
PendingIntent pendingIntent = PendingIntent.getService(
MainActivity.this,
3000,
intent,
PendingIntent.FLAG_NO_CREATE | PendingIntent.FLAG_IMMUTABLE
);
The pendingIntent returned comes out to be null even when I have set it using
private void scheduleExactAlarmBroadcast(Context ctx, Class<?> BrdcstRcvr, int requestCode, int afterInMs) {
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
Intent intent = new Intent(ctx, BrdcstRcvr);
PendingIntent pi = PendingIntent.getBroadcast(ctx, requestCode, intent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_MUTABLE);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && alarmManager.canScheduleExactAlarms()) {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + afterInMs, pi);
}
}
why is it coming out to be null? Is it not supposed to retrieve pendingIntents using requestCode?
I am trying to cancel the alarm using the requestCode of PendingIntent, however the PendingIntent returned by PendingIntent.getService() is coming out to always be null
user26358326 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.