关闭和打开手机后的 AlarmManager 对象
在我的应用程序中,我设置了一个闹钟,
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
...
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
除非我关闭然后打开手机,否则它工作正常。
更具体地说,假设在10点20分,我将闹钟设置为10点22分,然后我在10点21分关闭并打开手机,闹钟不起作用。
可能是什么问题?这是悬而未决的广播问题还是我应该设置alarmManager对象的一些标志以使其在这种情况下工作?
In my app, I set an alarm
AlarmManager alarmMgr = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
...
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
...
alarmMgr.set(AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), pendingIntent);
It works fine unless I turn off and turn on the phone.
To be more specific, let's say at 10:20, I set an alarm to 10:22 and I turn off and turn on the phone at 10:21, alarm won't work.
What might be the problem? Is that a broadcast issue of the pendingIntent there or should I set some flags of the alarmManager object for it to work in such conditions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于 AlarmManager 的文档说:
看来 Android 默认包含的 AlarmClock 即使在重新启动后也能工作。
重新启动后保持警报正常工作的一种方法是在启动完成后启动应用程序,并使用 AlarmManager 再次设置所有警报。
(事实上,您可能只想使用广播设置闹钟,而不是启动应用程序)
这里是一个关于在启动时为应用程序提供午餐的 StackOverflow 问题。
您还想通过读取源代码来查看默认的 AlarmClock 如何执行此操作。
您可以从此处阅读并下载它
The documentation about the AlarmManager says that :
It seems that the AlarmClock included by default by Android does work even after a reboot.
On way to keep your alarms working after a reboot, is to start your application on boot completed and set up all the alams again with the AlarmManager.
(In fact you may want to just setup your alarms using a Broadcast, not start your app)
Here is a StackOverflow question dealing about lunching an app on startup.
You wan also check out how the default AlarmClock does this by reading from the source.
You can read and download it from here