Search code examples
androidalarmmanagersimpledateformat

Schedule an alarm with AlarmManager using a given time on android


I want to schedule an alarm with AlarmManager, using input from my GUI.

As an example, I want the alarm to fire off at 18:15 so I have 2 int variables representing hours and minutes. I try to parse them with SimpleDateFormat but it doesn't seem to work. Any ideas why?

Edit: The problem is that the alarm doesn't seem to trigger. I'm guessing it's because the SDF isn't doing what I want it to do. But I dont know what to do to fix it.

public void setAlarm(){

    int hour = 18;
    int minute = 15;
    String myTime = String.valueOf(hour) + ":" + String.valueOf(minute);

    Date date = null;

    SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
    try {

        date = sdf.parse(myTime);

    } catch (ParseException e) {

        e.printStackTrace();
    }

    if (date != null) {
        timeInMs = date.getTime() + System.currentTimeMillis();
    }

    Intent intent = new Intent(this, AlarmClockRingRing.class);
    PendingIntent action = PendingIntent.getActivity(this, 0,intent, 0);

    AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
    am.set(AlarmManager.RTC_WAKEUP, timeInMs, action);
}

thankful for any help.

Marcus


Solution

  • The way you are doing it, you are setting an Alarm to 18 hours and 15 minutes from the current time, not to 18.15 of the present day.

    One way you could solve this is by creating a Date with the current time but with your desired hour and minutes values. Then just set your alarm at the that date. Like this:

        public void setAlarm(){
    
        int hour = 18;
        int minute = 15;
        String myTime = String.valueOf(hour) + ":" + String.valueOf(minute);
    
        Date date = null;
    
        // today at your defined time Calendar    
        Calendar customCalendar = new GregorianCalendar();
        // set hours and minutes
        customCalendar.set(Calendar.HOUR_OF_DAY, hour);
        customCalendar.set(Calendar.MINUTE, minute);
        customCalendar.set(Calendar.SECOND, 0);
        customCalendar.set(Calendar.MILLISECOND, 0);
    
        Date customDate = customCalendar.getTime();
    
        SimpleDateFormat sdf = new SimpleDateFormat("HH:mm", Locale.getDefault());
        try {
    
            date = sdf.parse(myTime);
    
        } catch (ParseException e) {
    
            e.printStackTrace();
        } catch (java.text.ParseException e) {
            e.printStackTrace();
        }
    
        if (date != null) {
            timeInMs = customDate.getTime();
        }
    
        Intent intent = new Intent(this, AlarmClockRingRing.class);
        PendingIntent action = PendingIntent.getActivity(this, 0,intent, Intent.FLAG_ACTIVITY_NEW_TASK);
    
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        am.set(AlarmManager.RTC_WAKEUP, timeInMs, action);
    }
    

    This code will launch an alarm at 18.15 of the current day. Be careful with setting a date that has already passed. Make sure you set a time in the future.