Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setAlarmClock without icon in notification bar

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.

like image 556
AVEbrahimi Avatar asked Oct 18 '15 05:10

AVEbrahimi


2 Answers

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:

  1. Use AlarmManager#setExactAndAllowWhileIdle and accept delays up to 15 minutes
  2. Use AlarmManager#setAlarmClock and accept the alarm clock
like image 163
techfly Avatar answered Sep 28 '22 01:09

techfly


You 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.

like image 44
greywolf82 Avatar answered Sep 28 '22 02:09

greywolf82