Broadcasts

Android broadcasts are messages following the publish-subscribe pattern. They are sent across the Android OS, with the internals hidden by the Android OS, so both publishers and subscribers see only a lean asynchronous interface for sending and receiving

  • PDF / 308,035 Bytes
  • 17 Pages / 504 x 720 pts Page_size
  • 64 Downloads / 174 Views

DOWNLOAD

REPORT


5

Broadcasts Android broadcasts are messages following the publish-subscribe pattern. They are sent across the Android OS, with the internals hidden by the Android OS, so both publishers and subscribers see only a lean asynchronous interface for sending and receiving messages. Broadcasts can be published by the Android OS itself, by standard apps, and by any other app installed on the system. Likewise, any app can be configured or programmed to receive the broadcast messages they are interested in. Like activities, broadcasts can be explicitly or implicitly routed, which is the responsibility of the broadcast sender to decide. Broadcast receivers are declared either in the AndroidManifest.xml file or programmatically. Starting with Android 8.0 (API level 26), the developers of Android have abandoned the usual symmetry between XML and programmatic declaration of broadcast receivers for implicit intents. The reason is that the general idea of imposing restrictions on processes running in background mode, especially related to broadcasts, resulted in a high load on the Android OS, slowing devices down considerably and leading to a bad user experience. For that reason, the declaration of broadcast receivers inside AndroidManifest.xml is now limited to a smaller set of use cases. Note  You will want to write modern apps that are runnable in Android 8.0 and newer. For that reason, take this broadcast limit for implicit intents seriously and make your app live within that limitation.

© Peter Späth 2018 P. Späth, Pro Android with Kotlin, https://doi.org/10.1007/978-1-4842-3820-2_5

43

44

CHAPTER 5: Broadcasts

Explicit Broadcasts An explicit broadcast is a broadcast published in such a way that there is exactly one receiver addressed by it. This usually makes sense only if both broadcast publishers and subscribers are part of the same app or, less frequently, part of the same app collection if there is a strong functional dependency among them. There are differences between local and remote broadcasts: local broadcast receivers must reside in the same app, they run fast, and receivers cannot be declared inside AndroidManifest.xml. Instead, a programmatical registration method must be used for local broadcast receivers. Also, you must use the following to send local broadcast messages: // send local broadcast LocalBroadcastManager.getInstance(Context).       sendBroadcast(...)

Remote broadcast receivers, on the other hand, can reside in the same app, they are slower, and it is possible to use AndroidManifest.xml to declare them. To send remote broadcasts, you write the following: // send remote broadcast (this App or other Apps) sendBroadcast(...)

Note  Local broadcasts should be favored over remote broadcasts for performance reasons. The apparent disadvantage of not being able to use AndroidManifest.xml to declare local receivers does not matter too much, since starting with Android 8.0 (API level 26) the use cases of declaring broadcast receivers inside the manifest files are limited anyway.

Explicit Local Broadca