android - 如何将闹钟设置为每天在 8 :00am 触发

标签 android calendar

我想设置每天早上 8:00 响起的闹钟。

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

am.setRepeating()

最佳答案

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

你还必须确保你有一个 BroadcastReceiver 用于启动完成,所以如果手机关机然后再打开,你可以重新安排闹钟:

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",就在如果您需要该信息。

为方便起见,这里有几个链接。 日历上的页面:

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,然后为了获得 triggerAtTime,你会得到一个与明天早上 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)

http://developer.android.com/reference/java/util/Calendar.html

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

关于android - 如何将闹钟设置为每天在 8 :00am 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7342492/

相关文章:

android - 从android中的周数和年份获取周开始和结束日期

python - GAE/Python ImportError timegm

java - 从 1970 到 2020.01.01 的毫秒数

android - vcalendar 与 icalendar

java - 使用 Java 中的方法将类作为类型传递给其他类

android - 在 Android 中从纬度/经度获取企业名称或地标

android颜色检测opencv

android - SQLite 数据库查询

android - 在 Android 中获取 SSLPeerUnverifiedException

javascript - 如何突出显示特定日期的日期并使用 keith-wood 日历在悬停时显示事件?