47
CHAPTER 5: Broadcasts
This is because the receiving class, here
MyReceiver
, is not part of the classpath. There is,
however, another construct we can use instead.
val intent = Intent()
intent.component = ComponentName("de.pspaeth.xyz",
"de.pspaeth.xyz.MyReceiver")
intent.action = "de.pspaeth.simplebroadcast.DO_STH"
// add other coords...
sendBroadcast(intent)
Here,
the first argument to
ComponentName
is the package string of the receiving package,
and the second argument is the class name.
Implicit Broadcasts
Implicit broadcasts are broadcasts with an undefined number of possible receivers. For
explicit
broadcasts, you learned that we had to build the corresponding intents by using
the constructor that points to the recipient component:
val intent = Intent(this,
TheReceiverClass::class.java)
. Contrary to that, for implicit
broadcast we no longer
specify the recipient but instead give hints on which components might be interested in
receiving it. Here’s an example:
val intent = Intent()
intent.action = "de.pspaeth.myapp.DO_STH"
sendBroadcast(intent)
Here, we actually express the following: “Send a broadcast message to all receivers that
are
interested in action
de.pspaeth.myapp.DO_STH
.” The Android OS determines which
components are eligible to receive such broadcast messages then;
this might result in zero,
one, or many actual recipients.
There are three decisions you must make before you start programming implicit broadcasts.
Dostları ilə paylaş: