BroadcastReceiver behavior when phone is asleep

狂风中的少年 提交于 2019-12-20 04:23:21

问题


I am not quite certain what the behavior of a BroadcastReceiver, registered in the manifest and enabled via PackageManager, is when the phone is asleep. The question arose because I need a receiver registered for broadcasts from WifiManager

<receiver
    android:name=".receivers.ScanResultsReceiver"
    android:enabled="false" >
    <intent-filter>
        <action android:name="android.net.wifi.SCAN_RESULTS" />
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />
    </intent-filter>
</receiver>

but what I want to know (as in links to the docs or some authoritative post in google groups) is which broadcasts are guaranteed to wake up a receiver when the phone has fallen asleep (as in left alone for quite some time) and keep the phone awake as long as onReceive() runs (which of course should not be too long to avoid ANR).
The receiver might well be the only component of the app running
As a bonus, I recently learned that some intents, flagged with FLAG_RECEIVER_REGISTERED_ONLY, are only delivered to dynamically registered Receivers - is there any place listing those intents ?


回答1:


I am not quite certain what the behavior of a BroadcastReceiver, registered in the manifest and enabled via PackageManager, is when the phone is asleep.

Most broadcasts do not wake up the device.

which broadcasts are guaranteed to wake up a receiver when the phone has fallen asleep (as in left alone for quite some time)

I doubt that you will find a definitive list somewhere. The only broadcasts that I can recall that fit your description are SMS_RECEIVED and any triggered via AlarmManager and a broadcast PendingIntent.

and keep the phone awake as long as onReceive() runs

It is possible that SMS_RECEIVED has this behavior, but I do not know that for certain. The AlarmManager scenario definitely does.

(which of course should not be too long to avoid ANR).

It needs to be far shorter than that, as it will freeze your UI, if you happen to have the foreground activity. Anything more than a couple of milliseconds needs to be delegated to a service with a background thread, such as my WakefulIntentService.

As a bonus, I recently learned that some intents, flagged with FLAG_RECEIVER_REGISTERED_ONLY, are only delivered to dynamically registered Receivers - is there any place listing those intents ?

No, beyond the source code. Ones that come to mind include: ACTION_SCREEN_ON, ACTION_SCREEN_OFF, and ACTION_BATTERY_CHANGED.



来源:https://stackoverflow.com/questions/16254420/broadcastreceiver-behavior-when-phone-is-asleep

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!