多报警,少工作,少不工作

Multiple Alarms, Few Works and Few does not

遇到了

的问题

AlarmManager

我开发了一种将两个警报添加到 AlarmManager Array 中的方法。我下面给出的方法在单击按钮时运行,并在每次单击 AlarmManager Array 两个中添加多达 10 个警报。

我的方法代码如下。

public void stupidAlarm()
{

    stupidPendingIntentOne  = PendingIntent.getBroadcast(context, listItemClickedPosition, stupidIntentOne, PendingIntent.FLAG_UPDATE_CURRENT);
    stupidPendingIntentTwo  = PendingIntent.getBroadcast(context, listItemClickedPosition+5, stupidIntentTwo, PendingIntent.FLAG_UPDATE_CURRENT);


    stupidAlarm[listItemClickedPosition]= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
    stupidAlarm[listItemClickedPosition+5]= (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);

    alarmOneTime    = settings.getString(AlarmOneTime, null);
    alarmTwoTime    = settings.getString(AlarmTwoTime, null);

    try
    {
        OneHr   = Integer.parseInt(muteTime.substring(0, 2));
        OneMin = Integer.parseInt(muteTime.substring(2, 4));
        TwoHr   = Integer.parseInt(ringerTime.substring(0, 2));
        TwoMin = Integer.parseInt(ringerTime.substring(2, 4));
    }
    catch(Exception ex)
    {
        Toast.makeText(context, ex.getMessage(), Toast.LENGTH_SHORT).show();
    }

    stupidCalOne.set(2015, Calendar.MAY, 2);
    stupidCalOne.set(Calendar.HOUR_OF_DAY, OneHr);
    stupidCalOne.set(Calendar.MINUTE, OneMin);
    stupidCalOne.set(Calendar.SECOND, 0);
    stupidCalOne.set(Calendar.MILLISECOND, 0);

    stupidCalTwo.set(2015, Calendar.MAY, 2);
    stupidCalTwo.set(Calendar.HOUR_OF_DAY, TwoHr);
    stupidCalTwo.set(Calendar.MINUTE, TwoMin);
    stupidCalTwo.set(Calendar.SECOND, 0);
    stupidCalTwo.set(Calendar.MILLISECOND, 0);



    stupidAlarm[listItemClickedPosition].set(AlarmManager.RTC_WAKEUP, stupidCalOne.getTimeInMillis(), stupidPendingIntentOne);
    stupidAlarm[listItemClickedPosition+5].set(AlarmManager.RTC_WAKEUP, stupidCalTwo.getTimeInMillis(), stupidPendingIntentTwo);
}

但问题是在 10 个警报中创建了一些有效而有些无效! 以下信息可能对这方面有所帮助

  1. AlarmOne 工作 1047 小时
  2. AlarmTwo 1048 小时有效
  3. AlarmThree 1049Hrs 不起作用
  4. AlarmFour 1050Hrs 工作两次
  5. AlarmFive 1051Hrs 不工作
  6. AlarmSix 1052Hrs 不工作
  7. AlarmSeven 1053Hrs 工作三次
  8. AlarmEight 1054 小时有效
  9. AlarmNine 1055Hrs 不起作用
  10. AlarmTen 1056Hrs 工作两次

我对 Calendar、Intent、PendingIntent 和 AlarmManagerArray 的声明

//for stupid alarm
public Calendar stupidCalOne;
public Calendar stupidCalTwo;
public Intent stupidIntentOne;
public Intent stupidIntentTwo;
public PendingIntent stupidPendingIntentOne;
public PendingIntent stupidPendingIntentTwo;
public AlarmManager[] stupidAlarm;

我在 onCreate 方法中的分配

//for stupid alarm
    stupidCalOne        = new GregorianCalendar();
    stupidCalTwo        = new GregorianCalendar();
    stupidIntentOne     = new Intent(context, OneAlarmReceiver.class);
    stupidIntentTwo     = new Intent(context, TwoAlarmReceiver.class);
    stupidAlarm         = new AlarmManager[10];

非常感谢任何帮助,提前致谢

允许 OS 批处理警报。

AlarmManager.set 状态的文档:

Note: Beginning in API 19, the trigger time passed to this method is treated as inexact: the alarm will not be delivered before this time, but may be deferred and delivered some time later. The OS will use this policy in order to "batch" alarms together across the entire system, minimizing the number of times the device needs to "wake up" and minimizing battery use. In general, alarms scheduled in the near future will not be deferred as long as alarms scheduled far in the future.

这就是为什么有些时间会被跳过,然后在一分钟后与另一个警报一起触发。

我怀疑它们之间的距离越远,系统对它们进行批处理的可能性就越小,但你仍然不能保证报警时间。

它继续:

With the new batching policy, delivery ordering guarantees are not as strong as they were previously. If the application sets multiple alarms, it is possible that these alarms' actual delivery ordering may not match the order of their requested delivery times. If your application has strong ordering requirements there are other APIs that you can use to get the necessary behavior; see setWindow(int, long, long, PendingIntent) and setExact(int, long, PendingIntent).

所以使用setWindow or setExact。但请注意 setExact:

Note: only alarms for which there is a strong demand for exact-time delivery (such as an alarm clock ringing at the requested time) should be scheduled as exact. Applications are strongly discouraged from using exact alarms unnecessarily as they reduce the OS's ability to minimize battery use.