Android 重复闹钟不起作用
这工作正常:
Intent intent = new Intent(HelloAndroid2.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid2.this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), pendingIntent);
这不起作用。我只听到一次闹钟。
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), 3 * 1000, pendingIntent);
我也尝试过这个,没有运气:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7000, pendingIntent);
是什么问题?
This works fine:
Intent intent = new Intent(HelloAndroid2.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(HelloAndroid2.this, 0,
intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), pendingIntent);
This doesn't work. I hear the alarm only time.
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + (12 * 1000), 3 * 1000, pendingIntent);
I have also tried this, no luck:
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 5);
alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 7000, pendingIntent);
What is the problem?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自 FLAG_ONE_SHOT 的 PendingIntent 文档:
因此,在第一次触发pendingIntent之后,它将被取消,并且下次尝试通过警报管理器发送它将会失败
尝试使用FLAG_UPDATE_CURRENT
From the PendingIntent doc for FLAG_ONE_SHOT:
So after the pendingIntent is fired the first time, it will be cancelled and the next attempt to send it via the alarm manager will fail
Try using FLAG_UPDATE_CURRENT
按顺序查看代码示例:
在第一个示例中,您使用的是 AlarmManager.set - 这严格用于一次性警报,所以是的,它只会触发一次。如果您想使用 AlarmManager.set,那么触发的代码应该做的最后一件事是设置一个新的警报(也应该使用一个新的 PendingIntent)。
在第二个示例中,您使用的是重复警报。您不需要每次触发时都需要创建一个新的 PendingIntent,因为操作系统会处理警报的重复部分。
您的警报没有理由不应该每 3 秒重复一次,因此我将开始查看您编写的用于处理警报的 BroadcastReceiver 实现。
检查您是否已正确实施。注释掉 onReceive() 方法中的所有代码,而只是让它写入日志消息。每次警报触发时,当您看到日志消息出现在 logcat 中时,请重新添加代码(保留日志消息),并将另一条日志消息添加到方法的末尾。这使您可以查看该方法执行需要多长时间 - 您希望它在警报再次触发之前完成,以避免任何意外的副作用。
顺便说一句,如果您想要重复警报,android.os.Handler 是一种更有效的方法,尽管通过 AlarmManager 设置的警报确实非常准确。
Looking at your code samples in order:
In your first sample you are using AlarmManager.set - this is strictly for one-off alarms so yes, it will only fire once. If you want to use AlarmManager.set then the last thing the code triggered should do is to set a fresh alarm (which should also use a fresh PendingIntent).
In your second example you are using a repeating alarm. You do not need to create a fresh PendingIntent each time this fires as the OS takes care of the repeating aspect of the alarm.
There is no reason why your alarm should not repeat every 3 seconds, so I would start looking at the BroadcastReceiver implementation you have written to handle the alarm.
Check that you've implemented it properly. Comment out all the code in the onReceive() method and instead just have it writing a log message. Once you see your log message appearing in the logcat every time the alarm fires, add your code back in (keeping the log message), and another log message to the end of the method. This allows you to see how long the method takes to execute - you want it to be finished before the alarm fires again to avoid any unexpected side effects.
As an aside, if you want a repeating alarm, android.os.Handler is a much more efficient approach although alarms set through AlarmManager do fire very accurately.