2014-06-18 1 views
0

특정 시간 동안 알람을 설정하고 취소하고 싶습니다. 다음 코드를 사용하여 TimePicker를 사용하여 동일한 작업을 수행하고 있습니다.알람 관리자 발사 실시간이 지나면 바로

public void setRecurringAlarm(int randomTimer,long mills, int i){ 
     Intent intent = new Intent(CreateAlarmActivity.this, AlarmReceiver.class); 
     intent.setData(Uri.parse("timer:" + i)); 
     PendingIntent pendingIntent = PendingIntent.getBroadcast(CreateAlarmActivity.this, 1253, intent, PendingIntent.FLAG_CANCEL_CURRENT| Intent.FILL_IN_DATA); 
     AlarmManager alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE); 

     alarmManager.set(AlarmManager.RTC_WAKEUP,mills, 
       pendingIntent); 
     Toast.makeText(CreateAlarmActivity.this, "Alarm "+i+" isSet", Toast.LENGTH_LONG).show(); 
    } 

참고 : 나는 오후 10:00에 대한 알람을 설정 -Suppose. 그것은 오후 10시에 잘 작동합니다. 그러나 다시 같은 코드 (오후 10시 이후)를 실행하면 즉, 알람이 설정된 시간이 지나고 알람을 다시 설정하기 위해 코드를 호출하면 즉시 실행됩니다. 왜 그런가? 나는 내가 잘못한 곳을 찾을 수 없다.

답변

1
public void scheduleAlarm() { 
    // time at which alarm will be scheduled here alarm is scheduled at 1 
    // day from current time, 
    // we fetch the current time in milliseconds and added 1 day time 
    // i.e. 24*60*60*1000= 86,400,000 milliseconds in a day 
    // Long time = new GregorianCalendar().getTimeInMillis()+24*60*60*1000; 

    Calendar cal = Calendar.getInstance(); 

    cal.add(Calendar.HOUR_OF_DAY, 20); 
    cal.add(Calendar.MINUTE, 00); 
    cal.add(Calendar.SECOND, 00); 

    Intent intent = new Intent(CreateAlarmActivity.this, AlarmReceiver.class); 

    // create the object 
    AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE); 

    // set the alarm for particular time 
    alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 
      PendingIntent.getBroadcast(this, 1, intentAlarm, 
        PendingIntent.FLAG_UPDATE_CURRENT)); 
    Toast.makeText(this, "Alarm Scheduled ", Toast.LENGTH_LONG) 
      .show(); 



} 

희망이 도움이됩니다 당신

0

2 일 :

  1. 당신이 시간 동안 다음의 mills 매개 변수에 같은 값을 사용하여 setRecurringAlarm(randomTimer, mills, i)를 호출하여 "그 코드를 리콜"하는 경우 알람이 과거에 발생하고 즉시 트리거됩니다 (트리거 시간이 지난 알람을 예약 한 경우 즉시 알람이 트리거 됨).

  2. PendingIntent.getBroadcast()으로 전화하여 | Intent.FILL_IN_DATA을 삭제하십시오. 이 매개 변수에는 PendingIntent 플래그 만 포함되어야하며 여기에는 속하지 않으므로 약간의 손상이있을 수 있습니다.

1

알람 시간이 현재 시간보다 이전인지 여부를 확인할 수 있습니다. 그렇다면 다음 날 (적어도 한 번 이상 알람을 켜거나 반복 알람을 설정하려는 경우)의 경고 시간을 설정하십시오.

Calendar cal = Calendar.getInstance(); 
cal.set(Calendar.HOUR_OF_DAY, hour); 
cal.set(Calendar.MINUTE, minute); 
cal.set(Calendar.SECOND, 0); 
if (System.currentTimeMillis() > cal.getTimeInMillis()) { 
    calendar.add(Calendar.DATE, 1); 
} 

alarmManager.set(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), pendingIntent); 
관련 문제