Introduction
Broadcasts are a message that follows the publish-subscribe pattern. Both the publisher and subscriber see only a lean asynchronous interface with the internals hidden.
An application can be configured or programmed to receive the broadcast messages. Broadcasts may be routed either explicitly or implicitly.
Explicit Broadcasts
The explicit broadcast is one published that is exactly the one that the receiver addressed in it. It makes sense since only both broadcast publisher and the subscribed members are part of the same collection. Many differences are available in the local and remote broadcasts. Local broadcast receivers must reside in the same application. A programmatic registration method is used for local broadcast receivers.
- LocalBroadcastManager.getnstance(Context)
- sendBroadcast(...)
The above code is used to send a local broadcast message and the below code is used to send a remote broadcast. Remote broadcasts reside in the same application and it is slower than a local broadcast.
Explicit Local Broadcast
The below code is used to send the local broadcast message to the local broadcast receiver in the application.
- val intent = Intent(this, MyReceiver::class.java)
- intent.action = "de.pspaeth.simplebroadcast.DO_STH"
- intent.putExtra("myExtra", "myextraval")
- Log.e("LOG", "Sending broadcast")
- Local BroadcastManager.getInstance(this).
- sendBroadcast(intent)
- Log.e("LOG", "Broadcast sent")
-
- class MyReceiver: BroadcastReceiver() {
- override
- fun onReceiver(context: context ? , intent : Intent ? ) {
- Toast.makeText(context, "Intent Detected.", Toast.Length_LONG).show()
- Log.e("LOG", "Received broadcast")
- Thred.sleep(3000)
- Log.e("LOG", "Broadcast done")
- }
- }
Explicit Remote Broadcast
In the remote broadcast, the data moves through the IPC channels and the remote broadcast messages to the application. The sender uses an explicit receiver class, the receiver must be declared in a programmatic way, and both sender and the receiver use the LocalBroadcastManager to send the messages to register with the receiver. This is referred to as a Local explicit broadcast. The sender uses the explicit receiver class and the receiver must be declared in AndroidManifest.xml. This is referred to as a Remote explicit broadcast.
- class MyReceiver: BroadcastReceiver() {
- override
- fun onREceive(context: context ? , intent : Intent ? ) {
-
- }
- }
Explicit broadcast to other Application
Both the sender and receiver of explicit broadcast live in the different applications. The first argument to ComponentName is the package string of the receiving package, and the second argument is the class name.
- val intent = Intent ()
- intent.component = ComponentName ("de.pspaeth.xyz" , "de.pspaeth.xyz.MyReceiver")
- intent.action = "de.pspaeth.simplebroadcast.DO_STH"
-
- sendBroadcast (intent)
Implicit Broadcast
The implicit broadcast is a broadcast with an undefined number of possible receivers. The Operating system determines which component is eligible for receiving such a broadcast message. A large number of predefined broadcast message types exist in android and broadcast senders and broadcast receivers join intent. If the system broadcast is involved, the remote broadcast is used.
- val intent = Intent ()
- intent.action = "de.pspaeth.myapp.DO_STH"
- sendBroadcast (intent)
Sending Implicit Broadcast
To send an implicit broadcast, it requires to specify the action and categories. The action is mandatory and remaining are in the optional case.sendBroadcast (intent). The above line is used to send the broadcast and to make the receiver get the message in a sequential way, each receiver may cancel and forward the messages to the next receiver.
Receiving Implicit Broadcast
Implicit broadcasts are received for a limited set of broadcast categories and adding a programmatic listener for implicit broadast is unrestricted in it.
- < application ..>
- <reciver android:name = ".MyReceiver">
- <intent-filter>
- <action android:name = "com.xyz.myapp.DO_STH" />
- <category android:name = "android.intent.Category.DEFAULT" / >
- <category android:name = "com.xyz.myapp.MY_CATEG" />
- <data android : scheme = "http"
- android:port = "80"
- android:host = "com.xyz"
- android:path = "items/7"
- android:mineType = "text/html" />
- <intent-filter>
- </receiver>
- </application>
For non-local broadcasting, permissions can be specified on the both side of the sender and the receiver. The broadcasting sending methods contain overloaded versions. This expresses sending a broadcast to a receiver that is protected by com.xyx.theapp.PERMISSION and the below code specifies if the permission on the sender side is not supported to receive the sender is protected in the way.
- val intent = Intent (this, MyRecevrer::class.java)
- sendBroadcast(intent)