That's how I create alarm
PendingIntent alarmIntent = PendingIntent.getBroadcast(getContext(), 1, intent, PendingIntent.FLAG_UPDATE_CURRENT);
AlarmManager alarm = (AlarmManager) getContext().getSystemService(ALARM_SERVICE);
Calendar startTime = Calendar.getInstance();
startTime.set(Calendar.HOUR_OF_DAY, hourOfDay);
startTime.set(Calendar.MINUTE, minute);
startTime.set(Calendar.SECOND, 0);
if (startTime.getTimeInMillis() < System.currentTimeMillis()) {
startTime.add(Calendar.DAY_OF_MONTH, 1);
}
long intendedTime = startTime.getTimeInMillis();
alarm.setRepeating(AlarmManager.RTC_WAKEUP, intendedTime, AlarmManager.INTERVAL_DAY, alarmIntent);
Method that I call in BroadcastReceiver:
private void startAlarm(Context context) {
Intent mainIntent = new Intent(context, MainActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(context, 1, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager myNotificationManager =
(NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(context);
builder.setSmallIcon(R.drawable.l_active)
.setContentTitle(context.getString(R.string.morn_title))
.setContentText(context.getString(R.string.morn_text))
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent);
myNotificationManager.notify(1, builder.build());
}
So I had a problem like here . When I was setting alarm to 9:51 and my time was 10:00 - alarm wasn't working. After adding
if (startTime.getTimeInMillis() < System.currentTimeMillis()) {
startTime.add(Calendar.DAY_OF_MONTH, 1);
}
Alarm works fine, but when I reboot phone - it doesn't work. I will try to explain with an example: Time on my phone is 23:50. I set alarm on 00:05, then I reboot the phone and wait until 00:05. And alarm doesn't fire. But! If I won't reboot my phone - everything works. And one more thing: I don't have problems with phone reboot when I do not set time on the past (for example, my time is 22:00, I set time to 22:05, reboot phone and it works)