2

I am using setExactAndAllowWhileIdle with an AlarmManager in API 25 and above. I am furthermore using a “standard” BroadcastReceiver for receiving the PendingIntent triggered by the Alarm. Moreover, WakefulBroadcastReceiver (which appears to be conceived more for services than alarms) has been deprecated in API 26.

  1. Has this class been replaced in API 26?
  2. Do I need to use an equivalent class (since it has been deprecated) with AlarmManager or using a standard BroadcastReceiver is sufficient to wake up the device when using an Alarm with setExactAndAllowWhileIdle?
  3. Finally, do I need to add a WAKE_LOCK permission to my Manifest when used with AlarmManager?
user229044
  • 232,980
  • 40
  • 330
  • 338
JF0001
  • 819
  • 7
  • 30

1 Answers1

1

The WakefuleBroadcastReceiver was originally used to handle operations which needed to be performed due to waking up (via alarm) where operations could take a while, such as making a network transfer. In order to do this, the WakefulBroadcastReceiver was used so a Service could be reliably started and would actually execute before the device went back into a low power state. It has been deprecated as the same type of behavior can be accomplished using other facilities, such as foreground services, JobScheduler or high priority push notifications. As far as wake locks go, it all depends on what you need to do when your alarm fires. This article may be helpful for understanding AlarmManager: http://po.st/7UpipA

Alarms have slowly been diminishing in their capabilities when in Doze mode, which was introduced in API 23. Starting with Oreo (API 26) background operations are placed under tighter restrictions to help with battery life.

If you are ok with using alpha level release software, the new WorkManager is the way to go as it handles a lot of the version dependencies for you based on the device where your code is running. It will automatically use JobScheduler, AlarmManager, etc. depending on what you need and what version of the OS you are executing on.

If you do not wish to use WorkManager, I suggest digging in to JobScheduler to see if it will meet your needs. You may have to do some API level checking and have your code use AlarmManager w/WakefulBroadcastReceiver on some platforms and JobScheduler on others.

Larry Schiefer
  • 15,687
  • 2
  • 27
  • 33
  • Thank you for your answer and time Larry. My alarm is actually literally an alarm and thus, does not need to do much work besides alarming the user. Thus, I believe that I should still be using an alarm from AlarmManager, right? – JF0001 May 28 '18 at 18:34
  • Yes and no. It really boils down to what you expect it to do when the device is in a lower power state. If you want it to alert the user and fully wake the device, you'll have to use `setExactAndAllowWhileIdle()` to get the alarm to trigger, but to notify the user and wake the screen you'll have to force the device out of Doze. – Larry Schiefer May 29 '18 at 19:35