Skip to content Skip to sidebar Skip to footer

Alarm Is Not Triggering On The Same Date

I want to reboot device on particular time so i am using alarm manager for that.below is the code of my activity. public class MainActivity extends AppCompatActivity { private

Solution 1:

There are two problems with your code:

  • Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 11);
    calendar.set(Calendar.MINUTE, 02);
    

    Here you're setting HOUR_OF_DAY and MINUTE but the SECOND field still has its initial value, so the alarm won't be triggered exactly on the minute (unless you called calendar.setTimeInMillis(System.currentTimeMillis()) exactly on the minute, which is quite unlikely).

    Should be something like this:

    Calendar calendar = Calendar.getInstance();
    calendar.setTimeInMillis(System.currentTimeMillis());
    calendar.set(Calendar.HOUR_OF_DAY, 11);
    calendar.set(Calendar.MINUTE, 2);
    calendar.set(Calendar.SECOND, 0); // setting seconds to 0
  • setRepeating() is inexact and doesn't work with Doze.

    For exact trigger times you should use exact alarms, check this answer for an example.

Solution 2:

This is from the documentation

Note: as of API 19, all repeating alarms are inexact. If your application needs precise delivery times then it must use one-time exact alarms, rescheduling each time as described above. Legacy applications whose {@code targetSdkVersion} is earlier than API 19 will continue to have all of their alarms, including repeating alarms, treated as exact.

Repeating alarms are always inexact so that they wont consume much battery. They may be fired a bit later than the expected time. If you want your alarm to exact, don't make it repeating. Set it again once you receive the alarm

Post a Comment for "Alarm Is Not Triggering On The Same Date"