I have followed the following link https://developer.android.com/training/scheduling/alarms.html.

RTC examples first one, to set an alarm for a specific time on all days. Even after following the same code, the alarm is not triggered at the time it is suppose to get triggered. Instead the notification gets triggered immediately after setting the time which is done with the help of a time picker. Following is my code snippet,

AlarmManager alarmMgr = (AlarmManager)     getSystemService(Context.ALARM_SERVICE);
Intent intent = new Intent(this, NotificationReceiver.class);
PendingIntent intentalarm = PendingIntent.getBroadcast(this, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY,4);
    calendar.set(Calendar.MINUTE, 30);

    alarmMgr.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), AlarmManager.INTERVAL_DAY, intentalarm);

Instead of setRepeating I also tried setInexactRepeating but there was no luck.I would also like to add that when I changed the

calendar.getTimeInMillis() and used SystemClock.elapsedRealtime() + 120 * 1000 the alarm triggered exactly after 2 minutes from the time it was set.

But when calendar.getTimeInMillis() is being used the intended working does not happen instead immediate triggering occurs.

Would be indeed very helpful if anyone can help me out find a solution.Only for a note, I could learn if alarm is set before current device time the alarm would be triggered immediately but that is not the case here.

NotificationReceiver.class is working fine as it is generated and appears on the title. But the time it appears is the cause of concern.

2

There are 2 answers

0
Sairam Rachapudi On

You are using both setTimeInMillis and hourofday and minute.

If you want your alarm to be triggered at 4:30 just add hourofday and minute.

Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY,4);
calendar.set(Calendar.MINUTE, 30);`
0
Insaf Mirza On

I could Identify the issue I was facing. I was using a TimePicker with 24 hour format. But I used SimpleDateFormat "hh:mm". What 'h' stands for is as follows "h->Hour in am/pm (1-12) ". So any alarm that I set at AM triggers correctly. When I set a time at PM since I had used 1-12 'h' format, alarm gets set for a AM time and since that time is already passed when compared to the device time the alarm/notification got triggered immediately.The right format to use was "HH:MM" where 'H' stands for "H->Hour in day (0-23)". This resolved my issue.

Checking the TimeinMillis on online epoch time converter helped me identify this issue.Once again thanks for helping me.