AlarmManager setRepeating 忽略间隔

发布于 2025-01-05 11:54:37 字数 999 浏览 1 评论 0原文

我使用此代码在我们的业务应用程序中设置警报:

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 another set() for over 5 minutes: launch at 2 minutes
  • use setInexactRepeating() instead of setRepeating(): launch at 2 minutes

Any insight would be helpful. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

呢古 2025-01-12 11:54:37

我立即想到的建议是——确保您不要在其他地方设置具有相同意图和不同值的警报。意图不必是同一个对象,请参阅 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.

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