Home > DeveloperSection > Beginner > BroadcastReceiver in Android

BroadcastReceiver in Android

Android Android  Broadcast 
0 Comment(s)
 1045  View(s)
Rate this:

BroadcastReceiver in Android

A broadcast receiver is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

Broadcast intents are Intent objects that are broadcast via a call to the sendBroadcast(), sendStickyBroadcast() or sendOrderedBroadcast() methods of the Activity class (the latter being used when results are required from the broadcast). In addition to providing a messaging and event system between application components, broadcast intents are also used by the Android system to notify interested applications about key system events (such as the external power supply or headphones being connected or disconnected).

Several system events are defined as final static fields in the Intent class. Other Android system classes also define events, e.g., the TelephonyManager defines events for the change of the phone state.

Intent.ACTION_BOOT_COMPLETED-: Boot completed. Requires the android.permission.RECEIVE_BOOT_COMPLETED permission.

Intent.ACTION_POWER_CONNECTED-: Power got connected to the device.

Intent.ACTION_POWER_DISCONNECTED-: Power got disconnected to the device.

Intent.ACTION_BATTERY_LOW-:   Triggered on low battery. Typically used to reduce activities in your app which consume power.

Intent.ACTION_BATTERY_OKAY-: Battery status good again.

Here is a sample which help you to clear the concepts of BroadCastReceiver in Android

1.      Create an Android project.

2.      Create an IncommingCallReceiver class and extends with BroadcastReceiver , and add following code in this class


package com.example.callstatebroadcastreceiver;


import android.content.BroadcastReceiver;

import android.content.Context;

import android.content.Intent;

import android.telephony.TelephonyManager;

import android.widget.Toast;


public class IncommingCallReceiver extends BroadcastReceiver {

     Context context;



     public void onReceive(Context context, Intent intent) {

           try {

                String state = intent.getStringExtra(TelephonyManager. EXTRA_STATE);

                // On InComing Call

                if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {

                     Toast.makeText(context, "Phone Is Ringing", Toast .LENGTH_LONG).show();


                // On Dial call

             if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {

                     Text(context, "Call Recieved", Toast.LENGTH_LONG)



                // On cancel or disconnect call

                if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {

           Toast.makeText(context, "Phone Is Idle", Toast.LENGTH_LONG)



           } catch (Exception e) {





3.      Add permission and define receiver in Manifest.xml file

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"



    android:versionName="1.0" >




        android:targetSdkVersion="17" />


 <uses-permission android:name="android.permission.READ_PHONE_STATE" />






        android:theme="@style/AppTheme" >



            android:label="@string/app_name" >


                <action android:name="android.intent.action.MAIN" />


          <category android:name="android.intent.category.LAUNCHER" />






            android:enabled="true" >


           <action android:name="android.intent.action.PHONE_STATE" />






4. No need to add extra code in MainActivity.class


package com.example.callstatebroadcastreceiver;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;


public class MainActivity extends Activity {



     protected void onCreate(Bundle savedInstanceState) {






     public boolean onCreateOptionsMenu(Menu menu) {

           // Inflate the menu; this adds items to the action bar if // it is present.

           getMenuInflater().inflate(R.menu.main, menu);

           return true;


Now run your application in android device

On Incoming call

On Dial call






Don't want to miss updates? Please click the below button!

Follow MindStick