使用AlarmManager进行多次报警

时间:2013-02-07 13:14:30

标签: android alarmmanager alarms

我目前正处于斗智斗勇的状态。

在我的应用程序中,用户可以通过选择他想要的工作日和时间来设置警报。

以下功能检查规则集并查找具有时间条件的规则。在这些情况下,它会为所选的每一天设置一个警报,并将其设置为重复(每7天)。

我已经阅读了很多关于多个警报含义的帖子(与单个警报相反)。我想我已考虑到以下因素:
- 为每个警报使用新的意图
- 为每个警报使用新的PendingIntent - 为每个警报使用不同的请求代码

请求代码收集在ArrayList中,因此另一个函数可以在程序退出时清除所有警报。

现在问题:我的闹钟不会响起。我能够追踪到这个功能的错误。 AlarmManager实例很好。我在最底部设置了一个测试报警器(在带有星号的行之后)。那火了。为什么???

clearAlarms();
int i=0;    
ArrayList<Rule> allRulesWithTimeFrames = new ArrayList<Rule>();
allRulesWithTimeFrames = Rule.findRuleCandidatesByTimeFrame();
for(Rule oneRule : allRulesWithTimeFrames)
{
    for(Trigger oneTrigger : oneRule.getTriggerSet())
    {
        if(oneTrigger.getTriggerType() == Event_Enum.timeFrame)
        {
            Calendar calNow, calSet;
            Time setTime;

            if(oneTrigger.getTriggerParameter())
                 setTime = oneTrigger.getTimeFrame().getTriggerTimeStart();
            else
                 setTime = oneTrigger.getTimeFrame().getTriggerTimeStop();

            calNow = Calendar.getInstance();            
            calSet = (Calendar) calNow.clone();
            calSet.set(Calendar.HOUR_OF_DAY, setTime.getHours());
            calSet.set(Calendar.MINUTE, setTime.getMinutes());
            calSet.set(Calendar.SECOND, 0);
            calSet.set(Calendar.MILLISECOND, 0);
            // At this point calSet would be a scheduling candidate. It's just the day the might not be right, yet.

            long milliSecondsInAWeek = 1000 * 60 * 60 * 24 * 7;

            for(int dayOfWeek : oneTrigger.getTimeFrame().getDayList())
            {
                // --------------------
                // Here's a lot of code I will spare you. It only changes
                // the variable calSetWorkingCopy.
                // --------------------

                SimpleDateFormat sdf = new SimpleDateFormat("E dd.MM.yyyy HH:mm");
                i++;
                String calSetWorkingCopyString = sdf.format(calSetWorkingCopy.getTime()) + " RequestCode: " + String.valueOf(i);
                Miscellaneous.logEvent("i", "AlarmManager", "Setting repeating alarm because of rule: " + oneRule.getName() + " beginning at " + calSetWorkingCopyString);

                Intent alarmIntent = new Intent(automationServiceRef, AlarmListener.class);
                PendingIntent alarmPendingIntent = PendingIntent.getBroadcast(automationServiceRef, i, alarmIntent, 0);
                centralAlarmManagerInstance.setInexactRepeating(AlarmManager.RTC_WAKEUP, calSetWorkingCopy.getTimeInMillis(), milliSecondsInAWeek, alarmPendingIntent);
                requestCodeList.add(i);
            }
         }
    }
}

        // ************* The below code is working *************
    // get a Calendar object with current time
    Calendar cal = Calendar.getInstance();
    // add 5 minutes to the calendar object
    cal.add(Calendar.SECOND, 10);
    SimpleDateFormat sdf2 = new SimpleDateFormat("E dd.MM.yyyy HH:mm");
    String calSetWorkingCopyString2 = sdf2.format(cal.getTime());
    Miscellaneous.logEvent("i", "AlarmManager", "Setting repeating alarm because of hardcoded test: beginning at " + calSetWorkingCopyString2);
    Intent alarmIntent2 = new Intent(automationServiceRef, AlarmListener.class);
    PendingIntent alarmPendingIntent2 = PendingIntent.getBroadcast(automationServiceRef, 0, alarmIntent2, 0);
    centralAlarmManagerInstance.setInexactRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, alarmPendingIntent2);
    requestCodeList.add(0);

1 个答案:

答案 0 :(得分:0)

尝试替换此

PendingIntent alarmPendingIntent2 = PendingIntent.getBroadcast(automationServiceRef, 0, alarmIntent2, 0);

用这个

PendingIntent alarmPendingIntent2 = PendingIntent.getBroadcast(automationServiceRef, 0, alarmIntent2, PendingIntent.FLAG_CANCEL_CURRENT);