Chapter 4: Broadcast Receivers
In This Chapter
Creating broadcast receivers
Organizing data from broadcast receivers
Restricting a receiver’s access
Chapter 3 of this minibook introduces a broadcast receiver for the purpose of running code at boot time. Here’s a summary of that chapter’s broadcast receiver news:
• When you send a broadcast, Android fires up all the receivers whose filters satisfy the intent.
• A broadcast receiver runs long enough to execute the code in the receiver’s onReceive
method. A receiver has no onCreate
, on Destroy
, or on
AnythingElse
methods — only onReceive
. After Android finishes executing the onReceive
method’s code, the broadcast receiver becomes dormant, doing nothing until an app sends another matching broadcast.
This chapter describes broadcast receivers in more detail.
Receivers 101
This chapter’s first example contains the world’s simplest broadcast receiver. To be precise, Listing 4-1 contains the receiver class (MyReceiver
, which extends BroadcastReceiver
), Listing 4-2 contains code to broadcast to the receiver, and Listing 4-3 contains the example’s AndroidManifest.xml
file.
Listing 4-1: A Simple Broadcast Receiver
package com.allmycode.rec1; ...