最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin0浏览0评论

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!

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论