AlarmManager does not work at a specific time, but ~ 5 minutes after a specified time ... Why? And it also works if I activate it by time. That is, if it is set at 10:00, and I activate it at 11:00, then in theory it should work tomorrow at 10:00, but it works every minute. And it will work tomorrow at ~ 10: 05

void setMorningAlarm(Context context){ AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE); Intent intent = new Intent(context, AlarmManagerBroadcastReceiver.class); PendingIntent pi = PendingIntent.getBroadcast(context, morningId, intent, 0); Calendar time = Calendar.getInstance(); time.set(Calendar.HOUR_OF_DAY, 8); time.set(Calendar.MINUTE, 0); alarmManager.setInexactRepeating( AlarmManager.RTC_WAKEUP, time.getTimeInMillis(), AlarmManager.INTERVAL_DAY, pi); Toast.makeText(context,"Morning Notify", Toast.LENGTH_SHORT).show(); } 
  • 2
    RTFM setInexactRepeating, setRepeating, set, setExact ... - Yura Ivanov
  • setRepeating () - sets repeating alarms with a fixed time interval setInexactRepeating () - sets repeating alarms without strict requirements for the accuracy of the repetition period. This method is preferable to the previous to save system resources - Bogdan Shulga

1 answer 1

For precise timing, use setRepeating() instead of setInexactRepeating .

Regarding the immediate response today, if the appointed time today is already in the past. The fact is that by doing as described in your question, for example, at 11.00, you get an expired Alarm , which is immediately executed. So embedded in the system. To avoid this, make sure that the response time is in the future. If it is already in the past, add a day. For example:

  Calendar timeToFireAlarm = Calendar.getInstance(); timeToFireAlarm .set(Calendar.HOUR_OF_DAY, 8); timeToFireAlarm .set(Calendar.MINUTE, 0); Calendar now = Calendar.getInstance(); if(timeToFireAlarm.getTimeInMillis < now.getTimeInMillis()) timeToFireAlarm.add(Calendar.DAY_OF_MONTH, 1); 
  • Yes thank you. I somehow got out ... only for me it only works for the next day from the alarmManager.setInexactRepeating( AlarmManager.RTC_WAKEUP, time.getTimeInMillis()+ TimeUnit.DAYS.toMillis(1), AlarmManager.INTERVAL_DAY, pi); - Bogdan Shulga
  • one
    Maybe it works in a day because you always add these days (+ TimeUnit.DAYS.toMillis (1)), even if the right time has not yet arrived today. If so, use a check like mine. - iramm