如何使用Android中的闹钟管理器设置多个闹钟
我正在构建一个警报应用程序。我已经成功实现了基本的报警功能。
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, sHour);
calendar.set(calendar.MINUTE, sMin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
long sdl = calendar.getTimeInMillis();
Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmList.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager ALARM1 = (AlarmManager)getSystemService(ALARM_SERVICE);
ALARM1.set(AlarmManager.RTC_WAKEUP, sdl, sender);
在我的应用程序中,用户可以选择天(星期日,星期一...)
来每周重复闹钟。 我正在尝试创建多个警报以每周重复一次,但不知道该怎么做。 我可以使用(重复)间隔创建它还是应该创建多个警报管理器?
I'm building an alarm application. I have successfully implemented basic alarm functions.
Calendar calendar = Calendar.getInstance();
calendar.set(calendar.HOUR_OF_DAY, sHour);
calendar.set(calendar.MINUTE, sMin);
calendar.set(calendar.SECOND, 0);
calendar.set(calendar.MILLISECOND, 0);
long sdl = calendar.getTimeInMillis();
Intent intent = new Intent(AlarmList.this, AlarmReceiver.class);
PendingIntent sender = PendingIntent.getBroadcast(AlarmList.this, 0, intent,PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager ALARM1 = (AlarmManager)getSystemService(ALARM_SERVICE);
ALARM1.set(AlarmManager.RTC_WAKEUP, sdl, sender);
In my application, user can select days (sunday,monday...)
to repeat the alarm weekly.
I'm trying to create multiple alarms to repeat weekly but don't know how to do it.
Can I create it using (repeat) interval or should I create multiple alarm managers?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您需要为
待处理意图
使用不同的广播ID
。像这样的东西this:
使用系统时间应该是每个待处理的唯一标识符
意图你开火。
You need to use
different Broadcast id's
for thepending intents
. Something likethis:
Using the system time should be a unique identifier for every pending
intent you fire.
来自文档:
多个
AlarmManager
无法解决您的问题。如果它们有多个不同的警报(不同时间和不同日期),那么每次触发前一个警报时,您都需要在BroadcastReceiver
中设置警报。您还需要保留
RECEIVE_BOOT_COMPLETED
并有一个BroadcastReceiver
来接收启动,以便在手机重新启动时您可以重新安排闹钟。From the docs:
Multiple
AlarmManagers
would not resolve your issue. If they have multiple different alarms (different times and different days), then you would need to set the alarm within theBroadcastReceiver
every time you fire off a previous alarm.You would also need to hold
RECEIVE_BOOT_COMPLETED
and have aBroadcastReceiver
to receive the boot so that if the phone is rebooted you can re-schedule your alarms.要设置多个闹钟,您需要每次定义您的
Intent
,以便将其与其他闹钟区分开来。我发现执行此操作的最简单方法是将Intent
的data
字段设置如下:代码的其余部分@Hassy31 保持不变。
请注意,根据文档,
PendingIntent.getBroadcast()
方法中的requestCode
参数(如 @parag 所建议)未使用,因此这不是正确的方法关于它。To set multiple alarms you need to define your
Intent
each time so that it is distinguishable from the others. The easiest way I find to do this is to set thedata
field of yourIntent
something as follows:The rest of your code @Hassy31 is fine as is and remains unchanged.
Note that the
requestCode
parameter in thePendingIntent.getBroadcast()
method (as suggested by @parag) is unused according to the documentation so this ain't the right way to go about it.为pendingIntent设置广播
id
set Broadcast
id
for pendingIntent使用 Android 闹钟管理器设置多个闹钟
Set multiple alarms using android alarm manager
我所做的与移动到链接列表中的下一个元素类似。我在数据库中保留一个 ReminderEntity,其中包含用户启用警报的一周中的所有天。
然后我只安排第一天。当第一天触发时,我会在那一刻安排第二天,依此类推。
如果用户在第一个警报发生之前删除它,也会发生同样的情况。在这种情况下,我从实体中清除已删除的日期,并为下一个可用的日期安排警报。
What I do is something similar to how you move to the next element in a linked list. I keep in database a ReminderEntity that has all the days of the week the user enabled the Alarm to go off.
Then I schedule the first day only. When the first day triggers then in that moment I schedule the next day and so on.
Same thing if the user deletes the first coming Alarm before it happens. In this case I clear the removed day from the entity and schedule an alarm for the next one available.
对挂起的意图使用不同的 requestCode 并使用 .FLAG_MUTABLE 作为 Flag 的类型
use different requestCode for the pending intents and use .FLAG_MUTABLE for type of Flag