Giorgi
Giorgi

Reputation: 30873

AlarmManager wakelock and goAsync in receiver

Introduction

According to AlarmManager Documentation

The Alarm Manager holds a CPU wake lock as long as the alarm receiver's onReceive() method is executing. This guarantees that the phone will not sleep until you have finished handling the broadcast. Once onReceive() returns, the Alarm Manager releases this wake lock. This means that the phone will in some cases sleep as soon as your onReceive() method completes. If your alarm receiver called Context.startService(), it is possible that the phone will sleep before the requested service is launched. To prevent this, your BroadcastReceiver and Service will need to implement a separate wake lock policy to ensure that the phone continues running until the service becomes available.

Android also provides WakefulBroadcastReceiver for solving this problem:

Helper for the common pattern of implementing a BroadcastReceiver that receives a device wakeup event and then passes the work off to a Service, while ensuring that the device does not go back to sleep during the transition.

This class takes care of creating and managing a partial wake lock for you; you must request the WAKE_LOCK permission to use it.

Starting from api level 11 BroadcastReceiver provides goAsync method which allows to keep the receiver running longer than 10 seconds and move time consuming code to background thread.

Question

If in the receiver of Alarm Manager's receiver (which inherits from BroadcastReceiver, not WakefulBroadcastReceiver) I call goAsync and start new thread for time consuming code there will the Alarm Manager keep the wake lock until I call finish on the pendingResult returned from goAsync or do I still need to acquire wake lock in the code that I run on the new thread?

Upvotes: 1

Views: 1535

Answers (1)

Piovezan
Piovezan

Reputation: 3223

"Starting from api level 11 BroadcastReceiver provides goAsync method which allows to keep the receiver running longer than 10 seconds and move time consuming code to background thread."

This is not true according to the documentation. To quote from the goAsync doc:

This does not change the expectation of being relatively responsive to the broadcast (finishing it within 10s)

Upvotes: 0

Related Questions