2013-02-07 2 views
0

현재 내 재치가 있습니다.AlarmManager가있는 여러 개의 알람

내 응용 프로그램에서 사용자는 원하는 평일과 시간을 선택하여 알람을 설정할 수 있습니다.

아래 기능은 규칙 집합을 검사하고 시간 기준이있는 규칙을 찾습니다. 이 경우 선택한 매일 알람이 설정되고 반복으로 설정됩니다 (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); 

답변

0

시도는 귀하의 제안이

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

덕분에이

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

을 대체한다. 나는 시간의 압력을 받고 프로그램을 수정하여 한 번에 하나의 알람 만 설정하고 onReceive() 중에 다음 알람을 설정했습니다. 그건 꽤 신뢰할 수있는 것으로 입증되었습니다. – Jens

관련 문제