Broadcast Receiver In Android

Introduction

 
Broadcast Receiver is one of the main components of the Android operating system. Broad cast receivers respond to broad cast messages from other applications or from the operating system itself. The Android operating system will broad cast messages to all the registered receivers whenever an event occurs in the system. Some of the Broadcast the Android system has is as follows.
  • android.intent.action.BATTERY_CHANGED
  • android.intent.action.BATTERY_LOW
  • android.intent.action.BOOT_COMPLETED
  • android.intent.action.CALL
  • etc
These are the built-in Broadcasts, and if any of the receivers are registered for these events then the system will send broadcast messages to all the receivers at the time the event happens. Here, I am going to describe how to create custom broadcast receivers. In this application, I have a button, on clicking that button a custom broadcast will be triggered and I will have a broadcast listener, this listener will catch this event and will show a Toast. For this first create the main layout for the activity like the following.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2. xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"  
  3. android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"  
  4. android:paddingRight="@dimen/activity_horizontal_margin"  
  5. android:paddingTop="@dimen/activity_vertical_margin"  
  6. android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">  
  7.   
  8.   
  9.    <Button  
  10. android:layout_width="wrap_content"  
  11. android:layout_height="wrap_content"  
  12. android:layout_centerInParent="true"  
  13. android:id="@+id/broadCastButton"  
  14. android:text="Send BroadCast"/>  
  15.   
  16.     <TextView  
  17. android:layout_width="wrap_content"  
  18. android:layout_height="wrap_content"  
  19. android:textAppearance="?android:attr/textAppearanceLarge"  
  20. android:text="Broad Cast Sample"  
  21. android:id="@+id/textView"  
  22. android:layout_alignParentTop="true"  
  23. android:layout_centerHorizontal="true" />  
  24. </RelativeLayout>  
Now bind the button for the java activity in the onCreate(),
  1. Button button = (Button) findViewById(R.id.broadCastButton);  
Now create a class which extends from the BroadCastListner like the following. This class will be called in response for the broadcast message fired.
  1. public class MyBroadCastListner extends BroadcastReceiver {  
  2. @Override  
  3. public void onReceive(Context context, Intent intent) {  
  4.         Toast.makeText(context,"Broad cast received",Toast.LENGTH_SHORT).show();  
  5.     }  
  6. }  
Now go to the manifest file and register our receiver there with the action name(this action name is custom).
  1. <receiver android:name=".MyBroadCastListner">  
  2.  <intent-filter>  
  3.  <action android:name="com.negociosit.CUSTOM_INTENT"></action>  
  4.  </intent-filter>  
  5. </receiver>  
Now this receiver is registered. Now we need to fire this broadcast; for that, implement the button click listener and do the code for broad cast like calling an Activity, the difference is here we are specifying the action name which we were given in our manifest and invoking it as sendBroadcast().
  1. button.setOnClickListener(new View.OnClickListener() {  
  2.   
  3. @Override  
  4.   
  5.  public void onClick(View view) {  
  6.   
  7.  Intent intent = new Intent();  
  8.   
  9.  intent.setAction("com.negociosit.CUSTOM_INTENT");  
  10.   
  11.  sendBroadcast(intent);  
  12.   
  13.  }  
  14.   
  15. });