如何设置闹钟在固定时间正确触发?
我有这段代码,
Calendar c = new GregorianCalendar();
c.add(Calendar.DAY_OF_YEAR, 1);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 22);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 30*1000;
long a=c.getTimeInMillis();
// Schedule the alarm!
AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
c.getTimeInMillis(), 1*60*60*1000, sender);
它没有在 23:22h 执行,
我做错了什么?我注意到firstTime和c.getTimeInMillis()在大小和长度上有很大不同。当我使用firstTime时,所以当设置为30秒时,闹钟执行得很好。
I have this code
Calendar c = new GregorianCalendar();
c.add(Calendar.DAY_OF_YEAR, 1);
c.set(Calendar.HOUR_OF_DAY, 23);
c.set(Calendar.MINUTE, 22);
c.set(Calendar.SECOND, 0);
c.set(Calendar.MILLISECOND, 0);
// We want the alarm to go off 30 seconds from now.
long firstTime = SystemClock.elapsedRealtime();
firstTime += 30*1000;
long a=c.getTimeInMillis();
// Schedule the alarm!
AlarmManager am = (AlarmManager)ctx.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
c.getTimeInMillis(), 1*60*60*1000, sender);
It is not executed at 23:22h
What I am doing wrong? I noticed firstTime and c.getTimeInMillis() differs a lot in size and length. When I use firstTime, so when set to 30 seconds, the alarm is executed well.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您正在使用
AlarmManager.ELAPSED_REALTIME_WAKEUP
标志,但您正在使用日历对象。这两件事不能同时发生。您需要使用 AlarmManager.RTC 或
AlarmManager.RTC_WAKEUP
如果您使用日历或日期对象指定闹钟时间(自 1970 年以来的毫秒数)。当您通过
SystemClock.elapsedRealtime()
指定闹钟时间(自手机启动以来的毫秒数)时,您可以使用AlarmManager.ELAPSED_REALTIME
或AlarmManager.ELAPSED_REALTIME_WAKEUP
。You are using the
AlarmManager.ELAPSED_REALTIME_WAKEUP
flag, but you are using a Calendar object. These two things don't go together.You need to use AlarmManager.RTC or
AlarmManager.RTC_WAKEUP
if you are specifying the alarm time using a Calendar or Date object (milliseconds since 1970).You use
AlarmManager.ELAPSED_REALTIME
orAlarmManager.ELAPSED_REALTIME_WAKEUP
when you are specifying the alarm time viaSystemClock.elapsedRealtime()
(milliseconds since the phone booted).如果您只想为下一次出现 hh:mm 设置闹钟,我可以使用以下代码成功
I had success with the following code, if you only want to set the alarm for the next occurance of hh:mm
要让闹钟在 30 秒后响起,请使用
获取当前时间,然后
编辑:
我认为问题是 ELAPSED_REALTIME_WAKEUP。这告诉 AlarmManager 您给它的时间是基于系统启动以来的时间。从现在起 30 秒内就可以了,但如果您希望它基于实时,您应该使用 RTC 或 RTC_WAKEUP。有关这些类型的完整说明,请参阅 javadoc。
To get the alarm to go off 30 seconds from now, use
to get the current time, and then
Edit:
I think the problem is the ELAPSED_REALTIME_WAKEUP. This tells the AlarmManager that the time you are giving it is based on time since system startup. This is fine for 30 seconds from now, but if you want it to be based on real time you should use RTC, or RTC_WAKEUP. See javadoc for full explanation of those types.