如何设置每天早上 8:00 响铃
我正在尝试设置每天早上 8:00 触发的闹钟。
我知道如何创建闹钟,但如何将其设置为每天上午 8:00 启动。
am.setRepeating()
I am trying to set an alarm to fire everyday at 8:00am.
I know how to create the alarm, but how will i set it to launch everyday at 8:00am.
am.setRepeating()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用日历并将其设置为您想要的适当时间。然后你会执行
cal.getTimeInMillis()
,并将其用于triggerAtTime,间隔将为24*60*60*1000 = 86,400,000你还必须确保你有一个BroadcastReceiver开机完成,所以如果手机关机再开机,您可以重新安排闹钟:
对于启动,您使用意图过滤器
"android.intent.action.BOOT_COMPLETED"
并且必须持有权限"android.permission.RECEIVE_BOOT_COMPLETED"
,以防万一您需要那个信息。为了您的方便,这里有几个链接。
日历页面:
http://developer.android.com/reference/java/ util/Calendar.html
以及 AlarmManager 上的页面:
http://developer.android.com/reference/android/app/AlarmManager.html
这是方法:
我猜对于类型,您会想要使用 ELAPSED_REALTIME,然后获取触发器AtTime,您将获得一个与明天早上 8:00 匹配的日历(称为 cal),然后执行
“然后”
,我不知道如何使用日历准确地获取明天上午 8:00,但我认为您会执行
cal.getInstance()
,然后cal.add(Calendar.DAY, 1)
,然后cal.set(Calendar.HOUR_OF_DAY, 8)< /code>
http://developer.android.com/reference/java/util/Calendar.html
我几乎没有使用过日历,所以我可能会出现一些错误,你可能必须使用它一点点,但这基本上就是需要做的。将来,如果你只是阅读文档并使用它一些,你通常就能弄清楚它。
You could use Calendar and set it for the appropriate time that you want. Then you would do
cal.getTimeInMillis()
, and use that for the triggerAtTime, and the interval would be 24*60*60*1000 = 86,400,000You would have to also make sure you have a BroadcastReceiver for boot completed, so if the phone is powered off then back on, you can re-schedule the alarm:
For boot, you use intent-filter
"android.intent.action.BOOT_COMPLETED"
and you must hold the permission"android.permission.RECEIVE_BOOT_COMPLETED"
, just in case you needed that info.For your convenience, here are a couple links.
The page on Calendar:
http://developer.android.com/reference/java/util/Calendar.html
And the page on AlarmManager:
http://developer.android.com/reference/android/app/AlarmManager.html
Here is the method:
And I guess for type, you would want to use ELAPSED_REALTIME, then to get triggerAtTime, you would get a Calendar (call it cal) that matched 8:00 AM tomorrow morning, then do
Then it would be
And I don't know how exactly to get tommorrow at 8:00 AM using Calendar, but I'm thinking you would do
cal.getInstance()
, thencal.add(Calendar.DAY, 1)
thencal.set(Calendar.HOUR_OF_DAY, 8)
http://developer.android.com/reference/java/util/Calendar.html
I have hardly used Calendar, so I may have some errors, and you may have to play with it a little, but that's essentially what would need to be done. In the future, if you just read the DOCs and play with it some, you'll usually be able to figure it out.