Updated 30 November 2021
Broadcast helps the Android application to send or receive a Broadcast message from the system or from another application. The system generates broadcasts when various events occur. So in this blog, we are discussing the Android local broadcast.
Android broadcast will help to send or receive any event at run time. For example, if you are developing an alarm-based application then at the time of the alarm with the broadcast event you can play the audio and perform any other action.
Broadcast are of two types:
Static broadcast receiver: These broadcast receivers are declared on the manifest file. These broadcast receivers work even if the application is closed.
Dynamic broadcast receiver: These are not declared on the manifest mostly used for the case when the application is running.
When we want to send or receive data in a different application at that time it is best to use the broadcast. But when the data for sharing is limited and is shared on the same application then it’s better to use Android local broadcast.
Android local broadcast will help to keep your data private as no other application will be able to send the broadcast to your application.
Add below dependency in your app-level build.gradle file for an older project like.
1 2 3 |
dependencies { compile ‘com.android.support:support-v4:23.4.0’ } |
For sending the local broadcast just create the instance of the LocalBroadcastManager
and call the sendBroadcast()
1 2 3 4 5 6 |
private fun sendLocalBroadcastForInformation(incrementId: String) { val intent = Intent("localBroadcastForData") intent.putExtra("keyFromNotification", true) intent.putExtra("incrementId", incrementId) LocalBroadcastManager.getInstance(this).sendBroadcast(intent) } |
For sending broadcast we have created intent and passed some information on the intent.
After the Intent creation we have passed the intent inside the sendBroadcast()
It’s time to receive the local broadcast that we have created in the previous step.
Create the broadcast receiver inside your receiving activity like below:
1 2 3 4 5 6 |
private val mMessageReceiver: BroadcastReceiver = object : BroadcastReceiver() { override fun onReceive(context: Context?, intent: Intent) { Toast.makeText(this, "Receiving the broadcast", Toast.LENGTH_SHORT) var incrementId: String = intent.getStringExtra(BundleKeysHelper.BUNDLE_KEY_INCREMENT_ID) ?: "" // Perform any action } |
Now register your receiver like below:
1 |
LocalBroadcastManager.getInstance(this).registerReceiver(mMessageReceiver, IntentFilter("localBroadcastForData")) |
Here registerReceiver()
is using the broadcast receiver and your intent action that we have created during the send broadcast.
Once you compleated the work through your broadcast we can unregister it like below:
1 |
LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver) |
In this blog, you have learned about the implementation of the Android local broadcast.
For more information regarding the Android broadcast follow the link.
Thanks for reading this blog. You can also check other blogs from here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.