Dulce Corrales
Dulce Corrales

Reputation: 13

How to correctly remove an alarm after a specific duration using a PendingIntent in Android?

I'm trying to set an alarm that should trigger after a specific number of days (or minutes for testing purposes) and remove data related to the alarm from SharedPreferences when the alarm triggers. I am using a PendingIntent to handle the alarm, but when the PendingIntent expires and the AlarmReceiver is triggered, the data (such as medicationIndex and uniqueID) arrives empty, and the alarm is not removed as expected.

Here is the code that sets the alarm:

val deleteIntent = Intent(context, AlarmReceiver::class.java).apply {
action = "com.example.ALARM_ACTION_DELETE"
putExtra("medicationIndex", medication.medicationIndex)
putExtra("uniqueID", medication.uniqueID)}
val deletePendingIntent = PendingIntent.getBroadcast(
    context.applicationContext,
    alarmID,  // Using the same alarmID for the delete alarm
    deleteIntent,
    PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
)

val alarmManager = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
val endTime = System.currentTimeMillis() + (1 * 60 * 1000L)  // Test with 1 minute
alarmManager.setExact(
    AlarmManager.RTC_WAKEUP,
    endTime,
    deletePendingIntent
)

And here is the code inside the AlarmReceiver:

 class AlarmReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context, intent: Intent) {
        val action = intent.action
val medName = intent.getStringExtra("medName")
        val quantity = intent.getStringExtra("quantity")
        val uniqueID = intent.getIntExtra("uniqueID", -1)
        if (action == "com.example.ALARM_ACTION_DELETE") {
            val medicationIndex = intent.getIntExtra("medicationIndex", -1)
            val uniqueID = intent.getStringExtra("uniqueID")

            if (medicationIndex != -1 && uniqueID != null) {
                // Logic to remove the alarm from SharedPreferences
                val sharedPreferences = context.getSharedPreferences("medications", Context.MODE_PRIVATE)
                val medications = MedicationUtils.getMedicationsFromSharedPreferences(sharedPreferences)

                if (medicationIndex < medications.size) {
                    MedicationUtils.deleteMedication(context, medicationIndex, medications)
                }
            } else {
                Log.e("AlarmReceiver", "Received empty data: medicationIndex or uniqueID is missing")
            }
        }
    }
}

When the alarm is triggered after the specified time, the data sent via putExtra (such as medicationIndex and uniqueID) appears to be empty or null in the AlarmReceiver, preventing the medication from being removed from SharedPreferences.

I verified that the values are not null before sending them in the Intent.
I made sure to use PendingIntent.FLAG_UPDATE_CURRENT to update the PendingIntent.

Question: What could be causing the data to be empty when the alarm triggers? How can I ensure that the correct data is passed to the AlarmReceiver so I can remove the alarm and its associated data from SharedPreferences? Any help or suggestions would be greatly appreciated!

Upvotes: 0

Views: 22

Answers (0)

Related Questions