简体   繁体   English

AlarmManager.setAlarmClock在错误的时间引发警报?

[英]AlarmManager.setAlarmClock throws alarms at wrong time?

I'm working on an app based on programming alarms with AlarmManager.setAlarmClock method. 我正在使用基于使用AlarmManager.setAlarmClock方法对警报进行编程的应用程序。 It mainly works, but in some cases alarms are thrown at a wrong time. 它主要起作用,但在某些情况下会在错误的时间发出警报。

For instance, in a Samsung device with android 5.1, I set 3 alarms at: 例如,在具有Android 5.1的Samsung设备中,我在以下位置设置了3条警报:

  • 18:00 18:00
  • 22:00 22:00
  • 6:00 (next day) 6:00(第二天)

And the result is 结果是

  • 18:00 -> Ok 18:00->好
  • 18:15 -> Thrown at 18:15 18:15-> 18:15投掷
  • 6:00 -> Thrown at 2:00!! 6:00-> 2:00投掷!

Here is the code for setting the alarm: 这是设置警报的代码:

if (Build.VERSION.SDK_INT >= 21) {
        Log.d(TAG, "Final (idle) at " + sdf.format(calendarObj.getTime()));
        AlarmManager.AlarmClockInfo info = new AlarmManager.AlarmClockInfo(calendarObj.getTimeInMillis(), pendingIntent);
        alarmManager.setAlarmClock(info, pendingIntent);
    }

And this is the code to generate the "calendarObj" instances (the iqDate object is an object with a specific Date, and the three alarms are set based on the date of this object. Just ignore it): 这是生成“ calendarObj”实例的代码(iqDate对象是具有特定日期的对象,并且基于该对象的日期设置了三个警报。只需忽略它):

Calendar c1 = Calendar.getInstance();
c1.setTime(iqDate.getTime());
c1.add(Calendar.DAY_OF_MONTH, -1);
c1.set(Calendar.HOUR_OF_DAY, 18);
c1.set(Calendar.MINUTE, 0);
c1.set(Calendar.SECOND, 0);

Calendar c3 = Calendar.getInstance();
c3.setTime(iqDate.getTime());
c3.add(Calendar.DAY_OF_MONTH, -1);
c3.set(Calendar.HOUR_OF_DAY, 22);
c3.set(Calendar.MINUTE, 0);
c3.set(Calendar.SECOND, 0);

Calendar c7 = Calendar.getInstance();
c7.setTime(iqDate.getTime());
c7.set(Calendar.MINUTE, 0);
c7.set(Calendar.SECOND, 0);
c7.set(Calendar.HOUR_OF_DAY, 6);

Any ideas why this is happening? 任何想法为什么会这样? It works most of the times, but sometimes alarms are not working properly... 它大多数时候都可以工作,但有时警报无法正常工作...

Thanks! 谢谢!

Beginning with API 19 (android.os.Build.VERSION_CODES.KITKAT) alarm delivery is inexact: the OS will shift alarms in order to minimize wakeups and battery use. 从API 19(android.os.Build.VERSION_CODES.KITKAT)开始,警报传递是不精确的:操作系统将转移警报,以最大程度地减少唤醒和电池消耗。 There are new APIs to support applications which need strict delivery guarantees; 有一些新的API支持需要严格交付保证的应用程序。 see setWindow(int,long,long,android.app.PendingIntent) and setExact(int,long,android.app.PendingIntent). 请参见setWindow(int,long,long,android.app.PendingIntent)和setExact(int,long,android.app.PendingIntent)。 Applications whose targetSdkVersion is earlier than API 19 will continue to see the previous behavior in which all alarms are delivered exactly when requested. targetSdkVersion早于API 19的应用程序将继续看到以前的行为,在该行为中,所有警报均在请求时准确传递。

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

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