0

我目前无计可施。

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

以下函数检查规则集并查找具有时间标准的规则。在这些情况下,它将为选定的每一天设置一个警报,并将其设置为重复(每 7 天)。

我已经阅读了很多关于多个警报的含义的帖子(与单个警报相反)。我想我已经考虑了以下几点:
- 为每个警报使用新的 Intent
- 为每个警报使用新的 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);
4

1 回答 1

0

尝试替换这个

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

有了这个

PendingIntent alarmPendingIntent2 = PendingIntent.getBroadcast(automationServiceRef, 0, alarmIntent2, PendingIntent.FLAG_CANCEL_CURRENT);
于 2013-02-07T14:40:25.277 回答