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

标签 android alarmmanager android-pendingintent

遇到了一个问题

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 1054Hrs 有效
  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];

任何帮助将不胜感激,提前致谢

最佳答案

允许操作系统批处理警报。

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).

所以使用setWindowsetExact .但请注意 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.

关于android - 多报警,少工作,少不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29999206/

相关文章:

java - 如何为我的 ClusterManager 使用 onClusterItemRendered 等函数?

Android:闹钟每 30 分钟播放一次,从 12:30 开始

android - 如何通过 PendingIntent 发送数据到广播?

java - 当闹钟时间与实时时间相差 1 小时的倍数时,PendingIntent 会触发 - Android

java - 将通知点击数据发送到子 Activity

android - 在控制台中应用 Logcat 过滤器 ^(?!.*(nativeGetEnabledTags)).*$

android - 如何确定约束布局的正确版本

android - 更新到 Android Jelly Beans 后重复的动画

android - 如何读取 "adb shell dumpsys alarm"输出

android - AlarmManager 以不一致的方式发送带有旧数据的未决 Intent