I am developing an android app that uses AlarmManager. The app has to be very accurate, but AlarmManager.setExact is available only from API 19 (Android Kitkat), and I have an old phone that its android version is 4 (API 16). I tried using a service, but it stops when I lock the phone. I need an alternative that works for APIs below 19. Does anyone have an idea?
Asked
Active
Viewed 619 times
1
-
Depending on your definition of "very accurate", nothing on Android is "very accurate" with respect to timing. – CommonsWare Jun 29 '17 at 17:46
-
@CommonsWare Very accurate means that the alarm will start in the second I want it to start – Paranoid Android Jun 29 '17 at 18:08
-
That won't happen with Doze mode and app standby on Android 6.0+. About the only thing that is fairly accurate (though perhaps not to the second) and reliable is `setAlarmClock()` on `AlarmManager`, and from the user's standpoint, that really should be reserved for alarm clock apps. – CommonsWare Jun 29 '17 at 18:10
-
@CommonsWare setAlarmClock is from android lollipop – Paranoid Android Jun 29 '17 at 18:29
-
Agreed. And, prior to Android 6.0, `set()`/`setExact()` were decent, though not necessarily down to the second, as there are lots of things going on in the device and it may take a moment to get around to forking a process for you and giving you control. – CommonsWare Jun 29 '17 at 18:32
-
@CommonsWare read my question again – Paranoid Android Jun 29 '17 at 18:49
-
You have not changed your question. Most likely, you should not be using Android for your project. For example, you might use a real-time OS (RTOS) on suitable hardware. – CommonsWare Jun 29 '17 at 19:03
1 Answers
1
Just set(). Before API 19, set was an exact set. setExact was added when set was made inexact.

Gabe Sechan
- 90,003
- 9
- 87
- 127
-
-
Like said above, nothing is exact on Android. Especially nothing to the second like you want. Android optimizes more for battery than that. – Gabe Sechan Jun 29 '17 at 18:25
-
But what is your targetSDK set to? That's what controls the behavior of set. If targetSDK>=19, set is inexact. If targetSDK<19, set is exact. This is independent of what the API level of the device is. See: https://developer.android.com/about/versions/android-4.4.html – Gabe Sechan Jun 29 '17 at 18:26
-
-
You aren't going to get exact second wakeups. Android just doesn't support it. – Gabe Sechan Jun 29 '17 at 19:06
-
-
Then use a newer version of the OS. What you want, you can't get reliably. – Gabe Sechan Jun 29 '17 at 19:09