Broadcasts In Kotlin

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.
  1. LocalBroadcastManager.getnstance(Context)    
  2.   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.
  1. sendBroadcast (...)  

Explicit Local Broadcast

 
The below code is used to send the local broadcast message to the local broadcast receiver in the application.
  1. val intent = Intent(this, MyReceiver::class.java)  
  2. intent.action = "de.pspaeth.simplebroadcast.DO_STH"  
  3. intent.putExtra("myExtra""myextraval")  
  4. Log.e("LOG""Sending broadcast")  
  5. Local BroadcastManager.getInstance(this).  
  6. sendBroadcast(intent)  
  7. Log.e("LOG""Broadcast sent")  
  8. //In receiver class     
  9. class MyReceiver: BroadcastReceiver() {  
  10.     override  
  11.     fun onReceiver(context: context ? , intent : Intent ? ) {  
  12.         Toast.makeText(context, "Intent Detected.", Toast.Length_LONG).show()  
  13.         Log.e("LOG""Received broadcast")  
  14.         Thred.sleep(3000)  
  15.         Log.e("LOG""Broadcast done")  
  16.     }  
  17. }   

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.
  1. class MyReceiver: BroadcastReceiver() {  
  2.     override  
  3.     fun onREceive(context: context ? , intent : Intent ? ) {  
  4.         //handle incoming broadcast    
  5.     }  
  6. }   

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.
  1. val intent = Intent ()    
  2. intent.component = ComponentName ("de.pspaeth.xyz" , "de.pspaeth.xyz.MyReceiver")    
  3. intent.action = "de.pspaeth.simplebroadcast.DO_STH"    
  4. // add other cooids    
  5. 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. 
  1. val intent = Intent ()    
  2. intent.action = "de.pspaeth.myapp.DO_STH"    
  3. 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.
  1. < application ..>    
  2.  <reciver android:name = ".MyReceiver">    
  3.  <intent-filter>    
  4.  <action android:name = "com.xyz.myapp.DO_STH" />    
  5.  <category android:name = "android.intent.Category.DEFAULT" / >    
  6.  <category android:name = "com.xyz.myapp.MY_CATEG" />    
  7.  <data android : scheme = "http"    
  8.            android:port  = "80"    
  9.            android:host = "com.xyz"    
  10.            android:path = "items/7"    
  11.            android:mineType = "text/html" />    
  12.  <intent-filter>    
  13.  </receiver>    
  14.  </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.
  1. val  intent = Intent (this, MyRecevrer::class.java)    
  2. sendBroadcast(intent)