JF0001
JF0001

Reputation: 839

Is WakefulBroadcastReceiver (or equivalent) necessary with AlarmManager

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?

Upvotes: 2

Views: 671

Answers (1)

Larry Schiefer
Larry Schiefer

Reputation: 15775

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.

Upvotes: 1

Related Questions