Learn About Broadcast Reciever in Android

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.
 
A Broadcast Receiver is used when a call is received. We then save the call details in our SQLite database. When the phone changes its state after receiving a call then the receiver notices the state change and saves the call into the SQLite database.
 
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.
  1. <receiver  
  2.     android:name="BatteryChanged">  
  3.     <intent-filter >  
  4. <action android:name="android.intent.action.BATTERY_CHANGED"/>  
  5.     </intent-filter>  
  6. </receiver> 
In Java file
 
On firing of the action android:name="android.intent.action.BATTERY_CHANGED" the onRecieve method will automatically call:
  1. public class BatteryCheck extends BroadcastReceiver  
  2. {  
  3.     public void onRecieve(Context context,Intent intent)  
  4.     {  
  5.         //Battery status will be received  
  6.     }  
Second way is to register dynamically
  1. private BroadcastReceiver reciever=new BatteryChange();  
  2. private IntentFilter filter=new IntentFilter(Intent.ACTION_BATTERY_CHANGED);  
  3. this.registerReciever(receiver,filter);  
  4. public class BatteryChange extends BroadcastReceiver  
  5. {  
  6.     public void onReceive(Context context,Intent intent)  
  7.     {  
  8.          
  9.     }  
How to create a custom event and BroadcastReciever
 
Register a BroadcastReciever in the Android menifest.xml file like this:
  1. <receiver  
  2.    android:name="Reciever">  
  3.    <intent-filter>  
  4.       <action android:name="ax.example.mybroadcast"/>  
  5.    </intent-filter>  
  6. </receiver> 
Create a Java file and write this:
  1. class Reciever extends BroadcastReceiver  
  2. {  
  3.     public void onRecieve(Context context,Intent intent)  
  4.     {  
  5.   
  6.     }  
Send Broadcast event
 
Send a broadcast event using the following:
  1. Intent intent = new Intent();  
  2. intent.setAction("ax.example.myBroadcast");  
  3. sendBroadCast(intent); 
Another way to define a receiver
  1. public class RecieverExample extends Activity   
  2. {  
  3.  private BroadcastReceiver receiver = new BroadcastReceiver()  
  4.  {  
  5.   @Override  
  6.   public void onReceive(Context context, Intent intent) {}  
  7.  };  
  8.  private IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);  
  9.  @Override  
  10.  public void onCreate(Bundle savedInstanceState)   
  11.  {  
  12.   super.onCreate(savedInstanceState);  
  13.   setContentView(R.layout.activity_main);  
  14.  }  
  15.  @Override  
  16.  protected void onResume()   
  17.  {  
  18.   this.registerReceiver(receiver, filter);  
  19.   super.onResume();  
  20.  }  
  21.  @Override  
  22.  protected void onPause()   
  23.  {  
  24.   this.unregisterReceiver(receiver);  
  25.   super.onPause();  
  26.  }  
Image of BroadcastReciever
 
android_broadcast_intent.jpg


Similar Articles