For the More Curious: Limitations on Broadcast Receivers

As you saw earlier in this chapter, there are limitations on broadcast receivers declared in your Android manifest that may cause your receiver to not be called. This behavior does not apply to receivers that you register dynamically using registerReceiver(…) and is only applicable when your app runs on Android Oreo (API level 26) and newer versions of Android.

The limitations on standalone broadcast receivers were introduced with the goal of improving battery life and performance on users’ devices. If a broadcast receiver is registered in your manifest and your app is not running, the system must spin up a new process whenever a broadcast needs to be delivered to this receiver. This act of spinning up processes is fairly unnoticeable if only one or two apps need to have their process started, but performance quickly deteriorates if many apps want to handle the same broadcast.

One example where this can directly harm user experience is if the user has apps that offer to automatically back up new photos taken by the camera. This would cause multiple processes to be started in the background whenever the user pressed the shutter button in the camera app, which would make the camera feel unresponsive to the user.

To alleviate this performance concern, new versions of Android beginning with Android Oreo no longer deliver implicit broadcasts to receivers declared in your manifest – with some exceptions. (Explicit broadcasts are not affected by these limitations, but recall that these kinds of broadcasts are only sent to one receiver and are rarely used.)

A number of system broadcasts are excluded from this rule. Broadcast receivers registered in your manifest for broadcasts including BOOT_COMPLETE, TIMEZONE_CHANGED, and NEW_OUTGOING_CALL will still receive those broadcasts. These and other broadcasts are exempt because they are either rarely sent or do not have an alternative way to perform functionality required by apps. You can find the full list of exemptions in the Android Developer documentation at developer.android.com/​guide/​components/​broadcast-exceptions.

As you saw when working through this chapter, broadcasts sent with a signature-level permission are also exempt from this restriction. This allows you to keep using standalone broadcast receivers that are private to your app and any other apps that you build using the same signature. Because only apps from one developer have permission to send these broadcasts, it is unlikely that there will be performance issues in other parts of the system caused by these kinds of broadcasts.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.238.254.78