broadcastreceiver - How to correctly remove an alarm after a specific duration using a PendingIntent in Android? - Stack Overflo

I'm trying to set an alarm that should trigger after a specific number of days (or minutes for tes

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!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742396533a4436059.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信