AlarmManager setRepeating 忽略间隔
我使用此代码在我们的业务应用程序中设置警报:
private void setupAlarm() {
final Context c = getApplicationContext();
final AlarmManager alarm =
(AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
final Intent i = new Intent(c, AlarmReceiver.class);
final PendingIntent sender =
PendingIntent.getBroadcast(c, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
final long startFromNow = System.currentTimeMillis()+10000;
final long timer = 5*60*1000;
alarm.setRepeating(AlarmManager.RTC_WAKEUP, startFromNow, timer, sender);
}
我无法理解为什么不遵守警报的间隔。第一个警报在 10 秒后启动(如预期),之后每隔 2 分钟(122 秒到 127 秒)启动一次,这是错误的。间隔是5分钟,还是我错了?
我在一个更简单的应用程序中使用了这个确切的代码:一个设置重复警报的活动,接收器只创建一个日志。在那里它起作用了。
是什么让 AlarmManager 的行为如此不同? 我尝试过:
- 使用
set()
并在警报接收器中使用另一个set()
超过 5 分钟:在 2 分钟时启动 - 使用
setInexactRepeating()< /code> 而不是
setRepeating()
:在 2 分钟后启动
任何见解都会有所帮助。谢谢!
I use this code to setup an alarm in our business application:
private void setupAlarm() {
final Context c = getApplicationContext();
final AlarmManager alarm =
(AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
final Intent i = new Intent(c, AlarmReceiver.class);
final PendingIntent sender =
PendingIntent.getBroadcast(c, 0, i, PendingIntent.FLAG_CANCEL_CURRENT);
final long startFromNow = System.currentTimeMillis()+10000;
final long timer = 5*60*1000;
alarm.setRepeating(AlarmManager.RTC_WAKEUP, startFromNow, timer, sender);
}
I cannot understand why the interval for the alarm is not respected. First alarm starts after 10 seconds (as expected), afterwards it starts every 2 minutes and a bit (122 seconds to 127 seconds), which is wrong. The interval is 5 minutes, or am I wrong?
I use this exact code in a simpler application: one activity that sets the repeating alarm and the receiver just creates a log. There it works.
What could make the AlarmManager act so different?
I have tried to:
- use
set()
and in the alarm receiver use anotherset()
for over 5 minutes: launch at 2 minutes - use
setInexactRepeating()
instead ofsetRepeating()
: launch at 2 minutes
Any insight would be helpful. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我立即想到的建议是——确保您不要在其他地方设置具有相同意图和不同值的警报。意图不必是同一个对象,请参阅 AlarmManager 中的设置方法文档。
Immediate suggestion that comes to mind - make sure you don't set an alarm with the same intent and different value elsewhere. The intent need not be the same object, see the set methods documentation in AlarmManager.