如何将闹钟设置为每天早上 8:00 响起 [英] How to set alarm to fire everyday at 8:00am

查看:100
本文介绍了如何将闹钟设置为每天早上 8:00 响起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置每天早上 8:00 触发的闹钟.

I am trying to set an alarm to fire everyday at 8:00am.

我知道如何创建闹钟,但我将如何将其设置为每天早上 8:00 启动.

I know how to create the alarm, but how will i set it to launch everyday at 8:00am.

am.setRepeating()

推荐答案

您可以使用日历并将其设置为您想要的适当时间.然后您将执行 cal.getTimeInMillis(),并将其用于 triggerAtTime,间隔将是 24*60*60*1000 = 86,400,000

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,000

您还必须确保您有一个已完成启动的 BroadcastReceiver,因此如果手机关机然后重新打开,您可以重新安排闹钟:

You 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:

在设备处于睡眠状态时保留已注册的警报(并且可以如果设备在此期间关闭,可选择唤醒设备),但如果关闭并重新启动将被清除.

Registered alarms are retained while the device is asleep (and can optionally wake the device up if they go off during that time), but will be cleared if it is turned off and rebooted.

开机时,你使用intent-filter "android.intent.action.BOOT_COMPLETED"并且你必须持有"android.permission.RECEIVE_BOOT_COMPLETED"权限,就在如果您需要该信息.

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/日历.html

AlarmManager 上的页面:

And the page on AlarmManager:

http://developer.android.com/reference/android/app/警报管理器.html

So how would that look inside AlarmManager.setRepeating()?

方法如下:

setRepeating(int type, long triggerAtTime, long interval, PendingIntent operation)

我猜对于类型,你会想要使用 ELAPSED_REALTIME,然后要获取 triggerAtTime,你会得到一个匹配明天早上 8:00 AM 的日历(称为 cal),然后执行

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

triggerAtTime = cal.getTimeInMillis()-Calendar.getInstance().getTimeInMillis()

那就是

alarmMan.setRepeating(AlarmManager.ELAPSED_REALTIME, triggerAtTime, 86400000, pendingIntent);

我不知道明天早上 8:00 使用日历的确切方法,但我想你会先执行 cal.getInstance(),然后执行 cal.add(Calendar.DAY, 1) 然后 cal.set(Calendar.HOUR_OF_DAY, 8)

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(), then cal.add(Calendar.DAY, 1) then cal.set(Calendar.HOUR_OF_DAY, 8)

http://developer.android.com/reference/java/util/日历.html

我几乎没有使用过日历,所以我可能会遇到一些错误,您可能需要稍微尝试一下,但这基本上是需要做的.以后,如果您只是阅读 DOC 并尝试一下,您通常就能弄明白.

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.

这篇关于如何将闹钟设置为每天早上 8:00 响起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆