Search code examples
androidkotlinandroid-intentalarmmanagerandroid-pendingintent

Is there a way to get the requestCode out of a pending intent once an alarm is triggered in my code?


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?


Solution

  • To answer your question

    Is there a way to get the requestCode out of a pending intent once an alarm is triggered in my code?

    No. You don't actually get the PendingIntent in your BroadcastReceiver, you only get the Intent that was wrapped by the PendingIntent. So the requestCode is not delivered to onReceive().


    You do it, as others have already mentioned, by putting the requestCode in the Intent as an "extra". Like this:

    val requestCode = newAlarmID.toInt()
    val pi = PendingIntent.getBroadcast(this, requestCode,
            Intent(intentName).putExtra("requestCode", requestCode),
            PendingIntent.FLAG_UPDATE_CURRENT)
    

    When the alarm triggers, get the requestCode from the Intent in onReceive() like this:

    override fun onReceive(context: Context?, intent: Intent?) {
        val requestCode = intent?.getIntExtra("requestCode", -1) ?: -1
        when (intent?.action) {
            "alarmTask" ->   handleAlarm(0, requestCode)
            "alarmTaskDaily" ->   handleAlarm(1, requestCode)
            "alarmTaskWeekly" ->   handleAlarm(2, requestCode)
        }
    }
    

    This will extract the requestCode from the Intent and pass it to your handleAlarm() method.

    NOTE: If, for whatever reason, the requestCode cannot be determined, the above code sets it to -1.