【问题标题】:PendingIntent fires when the alarm time is a multiple of 1 hour away from real time - Android当闹钟时间与实时时间相差 1 小时的倍数时,PendingIntent 触发 - Android
【发布时间】:2017-08-14 13:06:50
【问题描述】:

我正在创建一个闹钟应用程序。当我使用AlarmManager 设置的时间到达时,我可以设置PendingIntent,取消它们,并使用我的BroadcastReceiver 接收它们。然而,我发现了一个最近的问题。

以前,我已经能够为将来的任何时间设置警报,并且 BroadcastReceiver 在到达那个时间之前不会“接收” PendingIntent。我想我从来没有涵盖过要设置的警报恰好是 1 小时或更多(仅限整数)小时的情况。例如,当前时间是 11:54,我将闹钟设置为 12:54,或者 1:54、2:54 等。当我这样做时,BroadcastReceiver 接收到 PendingIntent 并执行我告诉它的操作去做。

为什么会这样?当我将分钟更改为不同的值时,它不会发生,只有当分钟相同时,应用程序的行为就像我为当前时间设置了闹钟一样。

这是我设置闹钟的方式:

public void scheduleAlarm(Context aContext) {
    AlarmManager am = (AlarmManager) aContext.getSystemService(Context.ALARM_SERVICE);
    Intent intent = new Intent(aContext, MyBroadcastReceiver.class);

    String id = this.getId().replaceAll("[^0-9]+", "");      // this.getId returns a string such as "alarm1". We only need the "1".

    PendingIntent alarmIntent = PendingIntent.getBroadcast(aContext, Integer.parseInt(id), intent, 0);

    // "this" in this context is the Alarm object. So you can get the hour and minute from the timepicker used to set the alarm
    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, this.getHour());
    calendar.set(Calendar.MINUTE, this.getMinute());

    long calendarTime = calendar.getTimeInMillis();
    am.setExact(AlarmManager.RTC_WAKEUP, calendarTime, alarmIntent);
}

【问题讨论】:

  • 难以置信。要检查这一点,请在调用 am.setExact() 之前添加日志记录,它会以用户可读的格式输出 calendarTime 中的时间值。然后检查时间是否真的是您认为应该的时间。
  • 你在这方面有什么收获吗?这仍然是一个问题吗?有更新吗?
  • 抱歉只是在度假,是的,由于某种原因它不再发生了。我重新安装了该应用程序,它运行良好。我无法重现问题
  • 好的,问题已经解决了。请删除该问题或回答您自己的问题,以便该问题不再出现在“未回答的问题”列表中。谢谢!

标签: java android alarmmanager android-pendingintent


【解决方案1】:

我检查了闹钟设置的时间,并确认它们不是现在的时间,所以它们不应该响起。在尝试设置多个警报并看到问题仍然存在后,我从手机中卸载了该应用程序,并重新安装了它。之后一切正常,并且仍然正常运行。

【讨论】:

    猜你喜欢
    • 2015-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-13
    • 2016-07-31
    相关资源
    最近更新 更多