Android local broadcast

Save

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.

Implementation

1. Initial setup

Add below dependency in your app-level build.gradle file for an older project like.

2. Sending Broadcast

For sending the local broadcast just create the instance of the LocalBroadcastManager and call the sendBroadcast()

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()

3. Receiving Broadcast

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:

Now register your receiver like below:

Here registerReceiver() is using the broadcast receiver and your intent action that we have created during the send broadcast. 

4. Unregister Broadcast

Once you compleated the work through your broadcast we can unregister it like below:

Conclusion:

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.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


Be the first to comment.

css.php

It was a successful collaboration. The Mobikul team provided the client everything they needed in a timely and high-quality manner. They were very professional, reliable, and accessible during the entire process. The client also saw no room for any improvements in the vendor's performance.

Vincenzo Carrano

Vincenzo Carrano

Administrator, Segshop

Talk to Sales

Global

Live Chat
Start a Project


    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home