Pro Android with Kotlin


Do we want to listen to system broadcasts?



Yüklə 4,6 Kb.
Pdf görüntüsü
səhifə54/231
tarix29.11.2023
ölçüsü4,6 Kb.
#138831
1   ...   50   51   52   53   54   55   56   57   ...   231
@de android telegram Pro Android with Kotlin Developing Modern Mobile

Do we want to listen to system broadcasts?
A large number of predefined broadcast message types exist for Android. 
Inside the Android SDK that you installed with Android Studio, at 
SDK_
INST_DIR/platforms/VERSION/data/broadcast_actions.txt
, you can find 
a list of system broadcast actions. If we want to listen to such messages, 
we just need to program the appropriate broadcast receivers as described 
later in the chapter. In the “System Broadcasts” section of the online text 
companion, you’ll find a comprehensive list of the system broadcasts.
Caution
Unless you are broadcasting to apps that have been built by yourself, this way of 
sending explicit broadcasts is of limited use only. The developer of the other app may easily decide 
to change class names, and then your communication to the other app using broadcasts will be 
broken.


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.



Yüklə 4,6 Kb.

Dostları ilə paylaş:
1   ...   50   51   52   53   54   55   56   57   ...   231




Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2024
rəhbərliyinə müraciət

    Ana səhifə