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.

<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)

    {

       

    }

}


How to create a custom event and BroadcastReciever

Register a BroadcastReciever in the Android menifest.xml file like this:

        <receiver

            android:name="Reciever">

            <intent-filter>

                <action android:name="ax.example.mybroadcast"/>

                </intent-filter>

        </receiver>

 

Create a Java file and write this:
 

 class Reciever extends BroadcastReceiver

 {

     public void onRecieve(Context context,Intent intent)

     {

 

     }

 }

 

Send Broadcast event


Send a broadcast event using the following:
 

         Intent intent =new Intent();

         intent.setAction("ax.example.myBroadcast");

                 sendBroadCast(intent);



Another way to define a receiver
 

public class  RecieverExample extends Activity {

 

    private BroadcastReceiver receiver = new BroadcastReceiver(){

 

        @Override

        public void onReceive(Context context, Intent intent) {

 

        }

    };

    private IntentFilter filter = new IntentFilter(Intent.ACTION_CONFIGURATION_CHANGED);

 

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    }

 

    @Override

    protected void onResume() {

 

        this.registerReceiver(receiver, filter);

        super.onResume();

    }

 

    @Override

    protected void onPause() {

 

 

        this.unregisterReceiver(receiver);

        super.onPause();

    }

 

}    


Image of BroadcastReciever


android_broadcast_intent.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all