I have an application with a RTC alarm. Currently I set the alarm to fire daily at 8:00AM. My problem is that when the alarm starts say at (4:00PM), it will consider the starting time (8AM) already in the past and hence will run.
I want the alarm to run only around 8:00AM, but if it is started late in the day, doesn't run. Any idea how?
I suggest you to use to calendar.add(Calendar.DAY_OF_YEAR, 1);
Study following code:
Calendar calendar = Calendar.getInstance();
// 9 AM
calendar.add(Calendar.DAY_OF_YEAR, 1); ///to avoid firing the alarm immediately
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
PendingIntent pi = PendingIntent.getService(context, 0,
new Intent(context, MyClass.class),PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
am.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(),
AlarmManager.INTERVAL_DAY, pi);