Why do we need to pass PendingIntent to both AlarmClockInfo and setAlarmClock()?

19 Views Asked by At

To schedule an alarm in Android, we need to pass pending intent to AlarmManager.AlarmClockInfo(triggerTime, pendingIntent) and AlarmManager.setAlarmClock(alarmClockInfo, pendingIntent).
Why setAlarmClock requires pending intent again? Wouldn't it be easier to get it from alarmClockInfo which it already takes?

Example of a code:

  private val alarmManager = context.getSystemService(AlarmManager::class.java)
    val pendingIntent = PendingIntent.getBroadcast(
      context,
      alarm.id, // request code
      Intent(context, AlarmReceiver::class.java),
      PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
    )
    val alarmClockInfo =
      AlarmManager.AlarmClockInfo(
        alarm.timestamp.toEpochSecond() * 1000,
        pendingIntent
      )
    alarmManager.setAlarmClock(
      alarmClockInfo,
      pendingIntent
    )

0

There are 0 best solutions below