Do I need to acquire a lock after calling BroadcastReceiver

In Pro Android 4 book By Satya Komatineni, Dave MacLean I found:

Android causes a partial tracking lock when a broadcast service is called and releases it when it returns from a service in the main thread

Does this mean that the Android OS guarantees that the device wakes up while passing onReceive from BroadcastReceiver ? In my case, the BroadcastReceiver should get the intention from the Google Play Service (GoogleLocationServices, or rather Geofences api ).

Where is it documented?

EDIT: I also found here :

because it is guaranteed that BroadcastReceiver.onReceive () will always be fully executed before the processor goes into sleep mode

0
source share
1 answer

There is no such thing as a broadcast service.

In addition, you need to read the entire post containing your second quote, as this is only for broadcast triggered by AlarmManager . This is the AlarmManager , not the Intent broadcast engine that contains WakeLock . In addition, as noted in this blog post, Dianne Hackborn has confirmed this behavior, and she is the main Android engineer.

Does this mean that the Adnroid OS guarantees that the device wakes up during the transition through OnReceive BroadcastReceiver?

Generally.

In my case, the BroadcastReceiver should get the intention from Google Play Services (GoogleLocationServices and, more precisely, Geofences api).

If the Google Play Services documentation does not contain any statements about your receiver's wakefulness behavior, you should assume that you are not inside WakeLock . If work is something that takes 1-2 ms, and therefore it is probably safe to do in onReceive() anyway, you can take a chance and skip WakeLock and hope for the best.

But usually, such a broadcast starts up more work, including disk I / O and / or network I / O, and you need to disconnect this work from the main application stream. Often you do this by delegating an IntentService , as it gives you a background thread with a token service to let the OS know that you are still doing some work here. And to make sure the device doesn't get on its feet to complete this work, use the WakefulBroadcastReceiver or my WakefulIntentService to hold WakeLock from an early time onReceive() until it completes on onHandleIntent() .

Where is this documented?

AFAIK, this is not so. Get used to it, since for complex systems, usually only a small part of the system behavior is recorded.

+1
source

All Articles