Alarm manager triggered immediately

后端 未结 2 1523
死守一世寂寞
死守一世寂寞 2021-02-04 05:26

Hi I am currently working with AlarmManager. I have written a code given below. As per code the AlarmManager should be triggered after 10 Sec, but here in my code the alarm mana

相关标签:
2条回答
  • 2021-02-04 06:00

    If you are creating PendingIntent of an alarm for past time it will be fired immediately. Example - Schedule alarm for today 8AM but executing code around 11AM will fire immediately.

    Solution:

    cal.add(Calendar.DATE, 1);
    
    long delay = 24 * 60 * 60 * 1000;
    alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), delay,pendingIntent);` 
    

    This will fire the event on next day at specified time (i.e 8AM);

    0 讨论(0)
  • 2021-02-04 06:03

    You are using an alarm type of ELAPSED_REALTIME_WAKEUP. That means that the second parameter to set() must be the number of milliseconds from now, where now is expressed as SystemClock.elapsedRealtime().

    If your goal is to have this occur 10000 milliseconds from the time you make the set() call, that set() call should be:

    alarmManager.set(alarmType, SystemClock.elapsedRealtime()+timeOrLengthofWait, alarmIntent);
    
    0 讨论(0)
提交回复
热议问题