0

I am using AlarmManager, trying to create an Alarm app for android. I noticed that setRepeating was not working when the phone sleeps. So, I tried setExactAndAllowWhileIdle.

But, I read this:

Unlike other alarms, the system is free to reschedule this type of alarm to happen out of order with any other alarms, even those from the same app. This will clearly happen when the device is idle (since this alarm can go off while idle, when any other alarms from the app will be held until later), but may also happen even when not idle. Note that the OS will allow itself more flexibility for scheduling these alarms than regular exact alarms, since the application has opted into this behavior. When the device is idle it may take even more liberties with scheduling in order to optimize for battery life.

I need accurate timings like an alarm clock. A user sets it for 6:00 am then ringing at 6:01 or 6:02 would be wierd! Not ringing at all because the phone is idle is catastrophic!

What can I do now?

gprathour
  • 14,813
  • 5
  • 66
  • 90
Mena
  • 3,019
  • 1
  • 25
  • 54

1 Answers1

0

Do not use repeating alarms for this purpose. They are not accurate/reliable enough. Schedule one alarm using set() or setExact() (depending on your target API level). When that alarm goes off, set the next one.

NOTE: Make sure that you use an alarm type that will wake the phone:

  • RTC_WAKEUP or
  • ELAPSED_REALTIME_WAKEUP
David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • Ok, I totally avoided repeating alarms mainly cus they don't work at all after the phone enters doze mode. But, I did try setting the alarm using "setExactAndAllowWhileIdle", but the problem is that if I set it for the first time at 6:00, it fires. Setting it after firing to 6:01 won't fire till after the passage of 10 to 15 minutes after the one before. I am testing using api 23. – Mena Jun 24 '17 at 12:28
  • Please use `setExact()`, not `setExactAndAllowWhileIdle()`. This will give you the behaviour that you want. – David Wasser Jun 24 '17 at 22:19
  • I tried the following to test per minute push notification: alarmMgr.setExact(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + 60*1000, alarmIntent); Works for 2 minutes, then starts to be totally inaccurate and delayed. Why? – Mena Jul 02 '17 at 09:12