I'm making a basic android/kotlin alarm app in my class. Once the alarm is triggered, I want to be able to then delete the alarm from a SQLite database or re-set the alarm if it is a recurring alarm. For both of these I need the requestCode from the pendingIntent as I am using the primary key of the alarm in the database as the requestCode. I'm not the only person to want this, and the solution seems to be using Intent.putExtras(), but my intent is not set up this way :
var intentName = when (newAlarmFrequency){
"Once" -> "alarmTask"
"Daily" -> "alarmTaskDaily"
"Weekly" -> "alarmTaskWeekly"
else -> null
}
And gets passed into the pi like this:
val pi = PendingIntent.getBroadcast(this, newAlarmID.toInt(), Intent(intentName), PendingIntent.FLAG_UPDATE_CURRENT)
This later gets handled like so:
val receiver = object: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
when (intent?.action) {
"alarmTask" -> handleAlarm(0)
"alarmTaskDaily" -> handleAlarm(1)
"alarmTaskWeekly" -> handleAlarm(2)
}
}
}
val filter = IntentFilter().apply {
addAction("alarmTask")
addAction("alarmTaskDaily")
addAction("alarmTaskWeekly")
}
registerReceiver(receiver, filter)
Is there a way to make this work the way I have done it or do I need to change the way I set the Intents?