2017-07-29 25 views
1

我正在创建闹钟应用程序。我可以设置PendingIntent's,取消它们,并在使用AlarmManager设置的时间达到时使用我的BroadcastReceiver接收它们。但是我发现了一个最近的问题。PendingIntent在闹钟时间为实时时间的1倍时的倍数时触发 - Android

之前,我可以在将来的任何时候设置闹钟,并且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); 
} 
+0

很难相信。要检查这一点,请在调用'am.setExact()'之前添加日志记录,该日志以用户可读格式输出'calendarTime'中的时间值。然后检查一下,看看时间真的是你认为应该的时间。 –

+0

你有没有遇到过这种情况?这仍然是一个问题吗?任何更新? –

+0

对不起只是一个假期,是的,因为某种原因它不再发生。我给了应用程序的全新安装,它工作正常。我不能重现问题 – Michael

回答

0

我检查了报警设置的时间,并确认他们是不是目前的时间,使他们不应该熄灭。在尝试设置多个闹钟并且发现问题仍然存在之后,我从手机中卸载了应用程序,并重新安装了该应用程序。之后一切正常,并仍能正常工作。