admin管理员组文章数量:1335550
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!
本文标签:
版权声明:本文标题:broadcastreceiver - How to correctly remove an alarm after a specific duration using a PendingIntent in Android? - Stack Overflo 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742396533a2467035.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论