如何更改 AlarmManager 警报?
我在我的应用程序中使用 AlarmManager
在适当的时间设置闹钟。我的应用程序中有多个闹钟,因此每次用户保存闹钟时,我都会找到下次应该播放哪个闹钟,并将该闹钟的 ID 作为意图的额外内容传递。这是我使用的代码:
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
intent.putExtra("alrmId", finalAlr);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 56, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + (finalAlrDay * 24 * 60 * 60 * 1000) + (finalAlrHr * 60 * 60 * 1000) + (finalAlrMin * 60 * 1000) + (finalAlrSec * 1000)), pendingIntent);
在这里,如果有任何旧的警报设置,我会取消,然后添加新的警报。所有闹钟都会在正确的时间播放,但问题是我在 intent.putExtra
中设置的 alrmId
值始终与我第一次设置时保持相同。
例如,如果我第一次设置闹钟,当时 alrmId
设置为“1”,那么无论我之后输入什么值,它都将始终保持不变。我尝试对其进行调试,并确保 intent.putExtra("alrmId", FinalAlr)
输入正确的值,因此这不是问题。问题是什么?
I am using AlarmManager
in my app to set an alarm at an appropriate time. I have multiple alarms in my app so every time the user saves an alarm, I find which alarm should be played next time and pass the ID of that alarm as an intent's extra. Here is the code I use for that:
Intent intent = new Intent(this, AlarmBroadcastReceiver.class);
intent.putExtra("alrmId", finalAlr);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 56, intent, 0);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.cancel(pendingIntent);
alarmManager.set(AlarmManager.RTC_WAKEUP, (System.currentTimeMillis() + (finalAlrDay * 24 * 60 * 60 * 1000) + (finalAlrHr * 60 * 60 * 1000) + (finalAlrMin * 60 * 1000) + (finalAlrSec * 1000)), pendingIntent);
Here, I cancel if there is any old alarm set and then add new one. All the alarms play at the right time but the problem is that the alrmId
value that I set in intent.putExtra
always remains the same as when I set it for the first time.
For example, if I set an alarm for the first time and at that time alrmId
is set to be '1' then it'll always stays the same no matter what value I put in after that. I have tried debugging it and I made sure that intent.putExtra("alrmId", finalAlr)
is putting in the right value so that is not the problem. What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在创建 <代码>PendingIntent。
Use
FLAG_UPDATE_CURRENT
when creating yourPendingIntent
.您还可以使用:
正如文档所述:
You could also use:
As the documentation says: