Broadcast receivers are registered for system announcements like headset plugin, power connected/disconnected etc. when an event occurs , Registered broadcast receiver are invoked via Intent. For example if you are implementing a media app and you're interested in knowing when the user connects or disconnects a headset, register for the ACTION_HEADSET_PLUG intent action.
Steps to work with BroadcastReceiver :-
1. Create a class as a child of BroadcastReceiver.
2. It is abstract class having an abstract method called onReceive().
3.From Activity class for which events you want to get the broadcast announcement , configure the events by using Intent Filter.
Intent Filter :- IntentFilter is used to fetch the information or Group of intents is called the Intent Filter.
registerReceiver(object_of_BR, intent_filter_object)
4 . If any one of the configured event is happen it will invoke the onReceive() method in BroadcastReceiver.
5. We can fire our own broadcast events by using sendBroadcast(intent_object) method.
6. We must configure our broadcast receiver in Manifest.xml file.
<receiver android:name="broadcst receiver class name"/>
example :-
0 Comments