broadcast receiver example in android with local broadcast

Broadcast Receiver Example In Android

Broadcast Receiver is a component that responds to a broadcast message sent by any other component. The mechanism to send message is through broadcast intents. Broadcasted message by clients can be received by many receivers. Receivers of the broadcast message are inherited from BroadcastReceiver class.

Broadcast receiver can be registered in AndroidManifest as well as can be created runtime.

Sending Broadcast:

Action is an identifier to identify the broadcast. You can change it as per your need. Receiver are also set for receiving pending intents set by Alarm Manager.

Receiving Broadcast:

Create our receiver class that will receive the broadcast with action "com.codeexa.broadcast.ACTION"

Go to File -> New -> Other -> BroadcastReceiver

Registering Receiver in Manifest

Registering Receiver at Run Time

Call registerBroadcast function whenever you want to start receiving broadcasts. Don’t forget to call unregisterReceiver when broadcasts are not required.

Local Broadcasts

LocalBroadcastManager can be used to register local broadcasts within the scope of app. It has numerous advantages and is more efficient over global receivers.

Register LocalBroadcastManager Receiver

Send LocalBroadcastManager Broadcast


Leave a Reply