To build an exact and reliable Alarm in Android OS 6, aka Marshmallow, I am forced to use setAlarmClock method of AlarmManager. The problem is this method displays an alarm icon in top status bar. Since my app needs an alarm to refresh some data at midnight, everyday, it will display alarm icon forever, which is not pleasant.
Unfortunately, this is completely intentional design by the Android developers in order to save battery. This motive becomes clear when you look through the documentation for AlarmManager methods. For example, this quote is taken from the documentation on AlarmManager#setExactAndAllowWhileIdle
:
These alarms can significantly impact the power use of the device when idle (and thus cause significant battery blame to the app scheduling them), so they should be used with care. To reduce abuse, there are restrictions on how frequently these alarms will go off for a particular application. Under normal system operation, it will not dispatch these alarms more than about every minute (at which point every such pending alarm is dispatched); when in low-power idle modes this duration may be significantly longer, such as 15 minutes. [emphasis mine]
Therefore, Google wants you to signal the user somehow that your app might be draining power by putting the little alarm clock in the status bar if you really really really need to have exact alarms. Quote from the documentation on AlarmManager#setAlarmClock
:
As such, these types of alarms can be extremely expensive on battery use and should only be used for their intended purpose.
Thus, you're left with these two options:
AlarmManager#setExactAndAllowWhileIdle
and accept delays up to 15 minutesAlarmManager#setAlarmClock
and accept the alarm clockYou need to use setExactAndAllowWhileIdle()
. If your app is not ignoring optimizations, you have 10 seconds to achieve your goal. If you need more time, a partial wakelock and/or internet access, you need to add the permission to request to the users to ignore optimizations.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With