48
CHAPTER 5: Broadcasts
How do we classify broadcast message types?
Broadcast senders and broadcast receivers join by intent filter matches,
just like activities do. As discussed in Chapter
3
when describing the intent
filters
for activities, the classification is threefold for broadcasts: first you
have an obligatory action specifier, second a category, and third a data-
and-type specifier that you can use to define filters. We describe this
matching procedure later in this chapter.
Are we heading for local or remote broadcasts?
If all the broadcasting happens
completely inside your app, you should
use local broadcasting for sending and receiving messages. For implicit
broadcasts, this will probably not be the case too often, but for large
complex apps, this is totally acceptable. If system broadcasts or
broadcasts
from other apps are involved, you
must
use remote broadcasts.
The latter is the default case in most examples, so you will see this pattern
quite often.
Intent Filter Matching
Broadcast receivers express their accepting broadcasts by means of declaring
action
,
category
, and
data specifiers
.
Let’s
first talk about
actions
. These are just strings without any syntax restriction. Looking
more thoroughly at them, you see that we first have a more or less stringently defined set
of predefined action names. We listed them all in Chapter
3
. In addition,
you can define
your own actions. A convention is to use your package name plus a dot and then an action
specifier. You are not forced to follow this convention, but it is strongly recommended to do
it that way so your apps do not get messed up with other apps. Without specifying any other
filter criteria, a sender specifying that particular action you specified in
the filter will reach all
matching receivers.
For an intent filter to match, the
action
specified on the receiver side
must match the
action
specified on the sender side. For implicit
broadcasts, zero, one, or many receivers might be addressed by one
broadcast.
A receiver may specify more than one filter. If
one of the filters contains
the specified
action
, this particular filter will match the broadcast.