简体   繁体   English

Android AlarmManager 小时警报运行错误间隔

[英]Android AlarmManager hour alarm running wrong interval

Im trying to set and run an alarm that run every hour, and is set by a few variables so as it will not run instantly, if the time is greater then the 58th minute The idea is to set it @ X hour and 58 minute, so it will run every hour, at the given minute(58).我试图设置并运行每小时运行一次的警报,并且由一些变量设置,因此它不会立即运行,如果时间大于第 58 分钟,想法是将它设置为 @ X 小时 58 分钟,所以它将每小时运行一次,在给定的分钟(58)。

Calendar calCurrent = Calendar.getInstance();

int time = 58 ;
Calendar calx = Calendar.getInstance();

calx.set(Calendar.MINUTE, time);
calx.set(Calendar.SECOND, 5);
if (calCurrent.get(Calendar.MINUTE) > time) {
            calx.add(Calendar.HOUR, +1);
}
System.out.println("Alarm is set to - " + calx.get(Calendar.HOUR)+":"+
        calx.get(Calendar.MINUTE));
alarmSwap = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
alarmSwap.setRepeating(AlarmManager.RTC_WAKEUP,
calx.getTimeInMillis(), 60 * 60 * 1000, pintent);

The code works and runs correctly for the 1st instance, then the alarm will for some reason run @ 0 minute the following hour.该代码在第一个实例中正常工作并运行,然后警报将出于某种原因在下一个小时运行@ 0 分钟。

Timeline looks like时间线看起来像


1:23 - Repeating Alarm Set for 1:58 (1 hour intervals)

1:58 - alarm is triggered

3:00 - alarm is triggered

I have no idea why this alarm is being triggered @ :00 for the last alarm.我不知道为什么会在最后一个警报 @:00 触发此警报。 It is not being called from anywhere else.它不是从其他任何地方调用的。

Any help is greatly appreciated.非常感谢任何帮助。

All alarms are resetting after the hour clocks over the hour-所有闹钟都在整点钟后重置-

  Calendar calnew = Calendar.getInstance(); calnew.add(Calendar.SECOND, +5); 
  alarm =      (AlarmManager) getSystemService(Context.ALARM_SERVICE);     
  alarm.setRepeating(AlarmManager.RTC_WAKEUP, calnew.getTimeInMillis(),900000 , pintent);

Timeline-时间轴-


1:20 triggered

1:35 triggered

1:50 triggered

2:00 triggered

2:15 triggered

2:30 triggered

From android documentation.来自安卓文档。

Note: as of API 19, all repeating alarms are inexact.注意:从 API 19 开始,所有重复的警报都是不准确的。 If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above.如果您的应用程序需要精确的交付时间,那么它必须使用一次性精确警报,如上所述重新安排每次。 Legacy applications whose targetSdkVersion is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact. targetSdkVersion 早于 API 19 的旧应用程序将继续将其所有警报(包括重复警报)视为准确。

repeating alarms are not exact after API 19. This improves androids performance as android groups alarms which are at close interval and wakes system once and finishes all alarms(from other applications also). API 19 之后的重复警报不准确。这提高了 androids 的性能,因为 android 将间隔很近的警报分组并唤醒系统一次并完成所有警报(也来自其他应用程序)。 If you really want exact alarms the you will have to set normal one time alarm, and then set alarm again in first alarm's call.如果您真的想要精确的闹钟,则必须设置正常的一次性闹钟,然后在第一个闹钟的呼叫中再次设置闹钟。

Try this code to repeat alarm every hour试试这个代码每小时重复一次闹钟

    alarmSwap.setInexactRepeating(AlarmManager.ELAPSED_REALTIME_WAKEUP,
    calx.getTimeInMillis(),
    AlarmManager.INTERVAL_HOUR, pintent);

Hope this will help.You can ask if you have any further queries.希望这会有所帮助。您可以询问是否有任何进一步的疑问。

This worked for me to trigger alarm after every 1hour这对我有用,每 1 小时后触发一次警报

alarmmgr = (AlarmManager) getSystemService(ALARM_SERVICE);
long interval = 60 * 60 * 1000; // 1 hour
xassert alarmmgr != null;
alarmmgr.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM