Search code examples
javaandroidalarmmanagerandroid-alarms

Android Alarm Manager not working with specific date and time


So I have been working on medication intake app, where I need to remind the user locally ( no internet/push notification needed) about taking their medication. I am using Android Alarm manager for this. Below is the code Note I am trying to schedule the alarm for a specific date: "13 July 2018 at 3h30 PM". I schedule and wait but the reminder didn't fire (so not broadcast) however if I used AlarmManager.ELAPSED_REALTIME_WAKEUP with a defined amount of millisecond it does fire ( but AlarmManager.RTC_WAKEUP just does not work) `

    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent myIntent;
    PendingIntent pendingIntent;
    long reminderDateTimeInMilliseconds = 000;

    myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);

    pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);

    //TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
    Calendar calendarToSchedule = Calendar.getInstance();

    calendarToSchedule.set(Calendar.YEAR, 2018);
    calendarToSchedule.set(Calendar.MONTH, 07);
    calendarToSchedule.set(Calendar.DAY_OF_MONTH, 13);
    calendarToSchedule.set(Calendar.HOUR_OF_DAY, 15);
    calendarToSchedule.set(Calendar.MINUTE, 30);

    reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();

    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){

        manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
    }
    else{

        manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
    }

`

Solution

  • So I figure out the issue, the main issue was the calendar month in Java is actually 0 index based so ( Jan:0 - Dec:11) so below is the updated code.

    AlarmManager manager = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
    Intent myIntent;
    PendingIntent pendingIntent;
    long reminderDateTimeInMilliseconds = 000;
    
    myIntent = new Intent(this,MedicationScheduleBroadCastReceiver.class);
    
    pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,PendingIntent.FLAG_UPDATE_CURRENT);
    
    //TODO : Reminder the user to take medication on the 13th July 2018 at 15:30
    // Note: For the month of July the int value will actuall be 6 instead of 7
    Calendar calendarToSchedule = Calendar.getInstance();
    calendarToSchedule.setTimeInMillis(System.currentTimeMillis());
    calendarToSchedule.clear();
    
    //.Set(Year, Month, Day, Hour, Minutes, Seconds);
    calendarToSchedule.set(2018, 06, 13, 15, 30, 0);
    
    
    reminderDateTimeInMilliseconds = calendarToSchedule.getTimeInMillis();
    
    if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O){
    
        manager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
    }
    else{
    
        manager.set(AlarmManager.RTC_WAKEUP, reminderDateTimeInMilliseconds, pendingIntent);
    }