繁体   English   中英

当闹钟时间与实时时间相距1小时的倍数时,会触发PendingIntent

[英]PendingIntent fires when the alarm time is a multiple of 1 hour away from real time - Android

我正在创建一个闹钟应用程序。 当达到使用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);
}

我检查了设置闹钟的时间,并确认它们不是当前时间,因此不应关闭。 尝试设置多个警报并发现问题仍然存在后,我从手机上卸载了该应用程序,并进行了全新安装。 之后,一切正常,并且仍然可以正常运行。

暂无
暂无

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

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