Search code examples
androidbroadcastreceiveralarmmanager

AlarmManager.setExact is not waiting the time interval


I have the following line

alarm.setExact(AlarmManager.RTC_WAKEUP,10000,pintent);

from what I understand I should receive a call from the AlarmManager to my Broadcast Receiver's onReceive after 10 seconds. How ever my Logs show that it reaches the onReceive method immediately. Is this normal? what method is triggered after the time specified in the setExact method above?


Solution

  • Change this line

    alarm.setExact(AlarmManager.RTC_WAKEUP,10000,pintent);
    

    into

    alarm.setExact(AlarmManager.RTC_WAKEUP,System.currentTimeMillis()+10000,pintent);
    

    so it will reach after 10 seconds

    EDIT:

    From docs setExact() will drain battery more..try to use set()

        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.SECOND, 10);
        alarmManager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), pintent);