2013-03-21 5 views
0

내 코드의이 부분에는 각 요일에 대해 부울이있는 일정이 있습니다. 그 날이 맞다면 그 날의 시작과 끝 알람이 있으며,이 알람은 그 날에 매주 트리거되어야합니다. 테스트 중에 현재 날짜 또는 나중의 일일 알람을 설정 한 경우, 다른 시간은 이미 발생한 요일처럼 또는 이전에 알람을 설정 한 경우에 작동합니다 테스트 할 때보 다 두 가지 알람은 어떤 이유로 든 생성시 사라집니다. 여기에 알람 생성을위한 코드입니다AlarmManager가 제대로 트리거되지 않았습니다.

public void createNewAlarm(int i) //int correlates to position in list 
    { 
     for(int j = 0; j < 7; j++) 
     { 
      if(tempmainfrag.mainObjectList.returnSchedule(i).returnDays()[j]) //if this day of the week has an alarm 
      { 
       ////beginning alarm stuff//// 
       int alarmid = (int)System.currentTimeMillis(); //creates unique id for the alarm attached to the object 

       int adjustedday = j+2; //makes time for DAY_OF_WEEK where sunday = 1, monday = 2, etc. 
       if (adjustedday == 8) 
        adjustedday = 1; 
       Calendar startcal = Calendar.getInstance(); 
       startcal.set(Calendar.DAY_OF_WEEK, adjustedday); 
       startcal.set(Calendar.HOUR_OF_DAY, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[0]); 
       startcal.set(Calendar.MINUTE, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[1]); 
       startcal.set(Calendar.SECOND, 0); 

       Intent intent = new Intent(this, SilenceHandler.class); 
       intent.putExtra("alarm_message", "Start!"); 
       intent.putExtra("vibratemode", tempmainfrag.mainObjectList.returnSchedule(i).returnVibrate()); 
       intent.setData((Uri.parse(String.valueOf(alarmid)))); 
       PendingIntent pendintent = PendingIntent.getBroadcast(this, alarmid, intent, PendingIntent.FLAG_UPDATE_CURRENT); 
       tempmainfrag.mainObjectList.returnSchedule(i).addStartPendIntent(alarmid); 

       AlarmManager alarmman = (AlarmManager)getSystemService(ALARM_SERVICE); 
       alarmman.setRepeating(AlarmManager.RTC_WAKEUP, startcal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendintent); 
       //120000 means every 2 mins 

       ////ending alarm stuff//// 
       if(tempmainfrag.mainObjectList.returnSchedule(i).nextday) //if end alarm ends on next day instead of same day 
       { 
        adjustedday++; 
        if (adjustedday == 8) 
         adjustedday = 1; 
       } 

       alarmid = (int)System.currentTimeMillis(); //creates unique id for the alarm attached to the object 

       Calendar endcal = Calendar.getInstance(); 
       endcal = Calendar.getInstance(); 
       endcal.set(Calendar.DAY_OF_WEEK, adjustedday); 
       endcal.set(Calendar.HOUR_OF_DAY, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[2]); 
       endcal.set(Calendar.MINUTE, tempmainfrag.mainObjectList.returnSchedule(i).returnTimes()[3]); 
       endcal.set(Calendar.SECOND, 0); 

       Intent intent2 = new Intent(this, SilenceHandler.class); 
       intent2.putExtra("alarm_message", "End!"); 
       intent2.putExtra("vibratemode", tempmainfrag.mainObjectList.returnSchedule(i).returnVibrate()); 
       intent2.setData((Uri.parse(String.valueOf(alarmid)))); 
       PendingIntent pendintent2 = PendingIntent.getBroadcast(this, alarmid, intent2, PendingIntent.FLAG_UPDATE_CURRENT); 
       tempmainfrag.mainObjectList.returnSchedule(i).addEndPendIntent(alarmid); 

       AlarmManager alarmman2 = (AlarmManager)getSystemService(ALARM_SERVICE); 
       alarmman2.setRepeating(AlarmManager.RTC_WAKEUP, endcal.getTimeInMillis(), AlarmManager.INTERVAL_DAY * 7, pendintent2); 
       //120000 means every 2 mins 
      } 
     } 

    } 

는 그리고 관련성 여부,하지만 여기에 알람 삭제에 대한 코드의 경우이 부분은 내 지식을 잘 동작하는 것하지만 나는 확실하지 않다 :

public void deleteAlarm(int i) 
{ 
    AlarmManager alarmman = (AlarmManager)getSystemService(ALARM_SERVICE); 

    Log.i("mydebug","Deleting alarm: The pending intent list null/not null is: " + tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList);//.pendintentlist.size()); 

    //delete start alarms 
    if (tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList != null) 
    { 
     Log.i("mydebug","Cancelling start alarm..."); 
     //cancels all alarms 
     for (int j = 0; j < tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList.size(); j++) 
     { 
      Intent intent = new Intent(this, SilenceHandler.class); 
      intent.putExtra("starttime",tempmainfrag.mainObjectList.returnSchedule(i)); 
      intent.putExtra("alarm_message", "Start!"); 
      intent.setData((Uri.parse(String.valueOf(tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList.get(j))))); 
      Log.i("mydebug","Alarm number " + (j+1) + " being cancelled."); 

      PendingIntent pendintent = PendingIntent.getBroadcast(this, tempmainfrag.mainObjectList.returnSchedule(i).startAlarmList.get(j), intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      alarmman.cancel(pendintent); 
     } 
    } 

    //delete end alarms 
    if (tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList != null) 
    { 
     Log.i("mydebug","Cancelling end alarm..."); 
     //cancels all alarms 
     for (int j = 0; j < tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList.size(); j++) 
     { 
      Intent intent = new Intent(this, SilenceHandler.class); 
      intent.putExtra("endtime",tempmainfrag.mainObjectList.returnSchedule(i)); 
      intent.putExtra("alarm_message", "End!"); 
      intent.setData((Uri.parse(String.valueOf(tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList.get(j))))); 
      Log.i("mydebug","Alarm number " + (j+1) + " being cancelled."); 

      PendingIntent pendintent = PendingIntent.getBroadcast(this, tempmainfrag.mainObjectList.returnSchedule(i).endAlarmList.get(j), intent, PendingIntent.FLAG_UPDATE_CURRENT); 
      alarmman.cancel(pendintent); 
     } 
    } 

    //deletes alarm intents from object 
    tempmainfrag.mainObjectList.returnSchedule(i).deleteIntents(); 
} 

답변

1

나는 AlarmManager 시스템을 더 잘 이해 한 후에 수정 사항을 찾아 냈습니다. 이 줄을 추가하여 수정했습니다.

Calendar checkdate = Calendar.getInstance(); //used so that alarm set to earlier in the week doesnt go off 
if (startcal.before(checkdate)) //see if alarm is for earlier in week 
    { 
     startcal.add(Calendar.DATE, 7); //makes alarm trigger next time it becomes that day instead of immediately 
    } 
관련 문제