Introduction
This article explains the Broadcast Receiver in Android.
A Broadcast Receiver is a receiver that helps to catch Android operating system specific events. A Broadcast Receiver is an Android component that listens for device orientation changes like SMS message received, phone call received, battery status received and WiFi comes on.
How to create a Broadcast Receiver
There are two ways to register and create a Broadcast Receiver. The first way is by the Android Manifest.xml file.
- <receiver
- android:name="BatteryChanged">
- <intent-filter >
- <action android:name="android.intent.action.BATTERY_CHANGED"/>
- </intent-filter>
- </receiver>
In Java file
On firing of the action android:name="android.intent.action.BATTERY_CHANGED" the onRecieve method will automatically call:
- public class BatteryCheck extends BroadcastReceiver
- {
- public void onRecieve(Context context,Intent intent)
- {
- //Battery status will be received
- }
- }
Second way is to register dynamically
- private BroadcastReceiver reciever=new BatteryChange();
- private IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
- this.registerReciever(receiver,filter);
- public class BatteryChange extends BroadcastReceiver
- {
- public void onReceive(Context context,Intent intent)
- {
- }
- }

Join the conversation! Your thoughts help the community grow.