如何设置每天早上 8:00 响铃

发布于 2024-12-03 18:26:44 字数 114 浏览 2 评论 0原文

我正在尝试设置每天早上 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2024-12-10 18:26:44

您可以使用日历并将其设置为您想要的适当时间。然后你会执行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

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

这是方法:

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

我猜对于类型,您会想要使用 ELAPSED_REALTIME,然后获取触发器AtTime,您将获得一个与明天早上 8:00 匹配的日历(称为 cal),然后执行

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

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.

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

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

Here is the method:

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

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()

Then it would be

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

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/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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文