【问题标题】:PendingIntent scheduled using AlarmManager.RTC type is still invoked in the sleep mode使用 AlarmManager.RTC 类型调度的 PendingIntent 仍然在睡眠模式下被调用
【发布时间】:2012-09-19 12:49:31
【问题描述】:

这是我用来为我的小部件设置警报的代码:

 private static void setAlarm(Context context) {
    Intent myIntent = new Intent(context, Widget.class);
    myIntent.setAction(AUTO_UPDATE);
    PendingIntent pendingIntent =
            PendingIntent.getBroadcast(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT);
    AlarmManager alarmManager = (AlarmManager) context.getSystemService(Service.ALARM_SERVICE);
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.add(Calendar.SECOND, 8);
    alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
            pendingIntent);
}  

但问题是即使在睡眠模式下,onReceive() 仍然会被意图触发。

虽然在使用setInexactRepeating 而不是setRepeating 之后,在睡眠模式下通话之间的延迟会增加至1 分钟,但这仍然很耗电池。

【问题讨论】:

  • AlarmManager.RTC 不应在睡眠模式下调用设备!我应该怎么做才能避免调用?
  • 您正在测试代码的 Android 操作系统版本和设备/模拟器是什么?
  • 我认为这个答案会对你有所帮助:stackoverflow.com/a/11273193/810368
  • 您确定您的测试环境中没有安排其他警报吗?系统可以由于其他事件而唤醒,然后因为它已经启动而触发你的。
  • 还有一个警报,每 24 小时触发一次。

标签: android alarmmanager android-pendingintent


【解决方案1】:

我相信您将闹钟设置为在日历时间后 8 秒触发,而您已将当前时间设置为提前 8 秒......所以您将闹钟设置为立即触发。

我看不出您有任何需要在此处使用日历的理由。此处日历仅用于跟踪未来 8 秒的时间:

Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(System.currentTimeMillis());
calendar.add(Calendar.SECOND, 8);

在此处创建警报以每 8 秒触发一次:

alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
        pendingIntent);

警报会继续每八秒触发一次。

我会尝试改变:

alarmManager.setInexactRepeating(AlarmManager.RTC, calendar.getTimeInMillis(), 8000,
        pendingIntent);

收件人:

alarmManager.setInexactRepeating(AlarmManager.RTC, System.currentTimeMillis(), 8000,
        pendingIntent);

如果您继续遇到问题,那么间隔可能就是问题所在。尝试将setRepeating() 更改为set() 看看是否是这种情况。

【讨论】:

    猜你喜欢
    • 2015-08-10
    • 1970-01-01
    • 1970-01-01
    • 2016-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多