2017-05-01 9 views
-1

AlarmManager를 사용하여 알람을 설정하고 timePicker에서 시간을 전달했습니다.다가오는 알람 즉시 트리거

주어진 시간에 알람을 설정하고 싶지만 설정하려고하는 시간이 지나면 알람이 으로 즉시 트리거됩니다.

예 : - 현재 시간이 오후 9:00이고 알람이 오후 7:00에 설정된 경우 알람이 즉시 트리거됩니다. 우리가 알람을 설정하는되는 시간이 경과 된 경우

다음은

public void startAlarm(Calendar c, int id){ 
    Toast.makeText(this, "Alarm in on", Toast.LENGTH_SHORT).show(); 
    Intent intent = new Intent(this,AlarmReciever.class); 
    intent.putExtra("id",id); 

    //int _id = (int)System.currentTimeMillis(); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,id,intent,PendingIntent.FLAG_ONE_SHOT); 
    alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), 1000*60,pendingIntent); 
    //alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent); 
} 

어떻게 알람의 트리거를 제어하려면 코드인가?

답변

1

캘린더 시간이 과거인지 검색하여 적절하게 조정해야합니다. 그래서 같이 :

public void startAlarm(Calendar c, int id){ 
    Toast.makeText(this, "Alarm in on", Toast.LENGTH_SHORT).show(); 
    Intent intent = new Intent(this,AlarmReciever.class); 
    intent.putExtra("id",id); 

    //Check if calendar is set in the past 
    if (c.getTimeInMillis() < System.currentTimeMillis()) { 
     //Add one day to the calendar (or whatever repeat interval you would like) 
     c.add(Calendar.DAY_OF_YEAR, 1); 
    } 

    //int _id = (int)System.currentTimeMillis(); 

    PendingIntent pendingIntent = PendingIntent.getBroadcast(this,id,intent,PendingIntent.FLAG_ONE_SHOT); 
    alarmManager = (AlarmManager)getSystemService(ALARM_SERVICE); 
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(), 1000*60,pendingIntent); 
    //alarmManager.set(AlarmManager.RTC_WAKEUP,c.getTimeInMillis(),pendingIntent); 
} 

난 당신이 한 일의 추가는 항상 트릭을 할 것입니다 경우 사용자가 잘 모르겠어요 시간을 설정 할 수 있도록하는 방법을 모르겠어요으로. 예를 들어 사용자가 달력에서 날짜를 선택할 수 있도록 허용하는 경우 과거의 연도가 될 수 있습니다. 원하는 반복 간격 및 사용자가 시간을 설정하는 방법에 따라 추가하는 시간을 조정해야합니다.