Android Programming The Big Nerd Ranch Guide by Bill Phillips, Chris Stewart, Kristin Marsicano (z-lib.org)

(gtxtreme123) #1

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

Free download pdf