Alarm Manager with 2 pending intents only 1 works?

后端 未结 2 682
北恋
北恋 2021-01-25 22:57

I have 2 alarms set, one for notifications, and the other one to do some tasks. My problem is that only one alarm seems to work( the notifications service one, the first alarm s

相关标签:
2条回答
  • 2021-01-25 23:24

    You're most likely seeing an issue because a Service is not guaranteed to run when triggered by an alarm, due to power save. If your device is on battery and idle when that alarm goes off, it will not trigger until the next time the device is full on or on AC power. You'll need to use a BroadcastReceiver which holds a wake lock which is then released by the Service when it is done. The WakefulBroadcastReceiver makes this a little easier to handle. This article will help provide more details.

    0 讨论(0)
  • 2021-01-25 23:30
    Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 14); // For 1 PM or 2 PM
        calendar.set(Calendar.MINUTE, 57);
        calendar.setTimeInMillis(System.currentTimeMillis());
    

    You are setting the HOUR_OF_DAY and the MINUTE but them you override that by calling setTimeInMillis(System.currentTimeMillis());

    After that you set the alarm with the calendar.getTimeMillis() value which is already in the past, so the alarm is cancelled I think.

    0 讨论(0)
提交回复
热议问题