29
Broadcast Intents

In this chapter you will polish PhotoGallery in two big ways. First, you will make the app poll for new search results and notify the user if new results are found, even if the user has not opened the application since booting the device. Second, you will ensure that notifications about new results are posted only if the user is not interacting with the app. (It is annoying and redundant to both get a notification and see the results update in the screen when you are actively viewing an app.)

In making these updates, you will learn how to listen for broadcast intents from the system and how to handle such intents using a broadcast receiver. You will also dynamically send and receive broadcast intents within your app at runtime. Finally, you will use ordered broadcasts to determine whether your application is currently running in the foreground.

Regular Intents vs Broadcast Intents

Things are happening all the time on an Android device. WiFi is going in and out of range, packages are getting installed, and phone calls and text messages are coming and going.

When many components on the system need to know that some event has occurred, Android uses a broadcast intent to tell everyone about it. Broadcast intents work similarly to the intents you already know and love, except that they can be received by multiple components, called broadcast receivers, at the same time (Figure 29.1).

Figure 29.1  Regular intents vs broadcast intents

Figure shows the difference between intents and broadcast intents.

Activities and services should respond to implicit intents whenever they are used as part of a public API. When they are not part of a public API, explicit intents are almost always sufficient. Broadcast intents, on the other hand, only exist to send information to more than one listener. So while broadcast receivers can respond to explicit intents, they are rarely, if ever, used this way, because explicit intents can only have one receiver.

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

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