Android Broadcastreceiver Example Code Description

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

Android Broadcastreceiver Example:

Here we will learn the simplest way for how to use  Android broadcastreceiver using ‘Android Broadcastreceiver Example‘. It is very easy to use broadcastreceiver in Android. Here we will take an example for incoming SMS. We will register a broadcast receiver for incoming SMS. Then our application will receive notification when a new incoming SMS will come to the phone.
 That means we are going to discuss about incoming SMS listener using Android broadcastreceiver.

How to use Android Broadcastreceiver with Example

To make this example very simple, let’s take an example for incoming SMS. we will register the broadcast receiver for incoming SMS and receive the notification when a new SMS will come to our phone.  Using Android broadcast receiver is a 3 step process.

1. First Create your Receiver Class

First create your own Receiver class by extending from Android default BroadcastReceiverclass. Then @Override the onReceive() method in your class. This onReceive() method will be called when a new SMS will come to the phone. Below is my Receiver Class for getting incoming SMS notification. In the onReceive() method we will get incoming SMS details. We will show the incoming SMS details using an Android Toast in our application.

package com.techblogon.androidbroadcastrecieverexample;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.util.Log;
import android.widget.Toast;

//Here is your broadcast receiver class
public class MyBroadcastReceiver extends BroadcastReceiver{
private static final String TAG = “MyBroadCastReceiver”;
public void onReceive(Context context, Intent intent) {
Log.i(TAG,”OnReceive ++>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>”);
Bundle bndl = intent.getExtras();
SmsMessage[] msg = null;
String str = “”;
if (null != bndl)
//—retrieve the SMS message received—
Object[] pdus = (Object[]) bndl.get(“pdus”);
msg = new SmsMessage[pdus.length];
for (int i=0; i<msg.length; i++){
msg[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += “SMS From ” + msg[i].getOriginatingAddress();
str += ” :\r\n”;
str += msg[i].getMessageBody().toString();
str += “\n”;
//—display incoming SMS as a Android Toast—
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

2. Register your Broadcastreceiver to Get Notification.

There are two different ways to register the broadcastreceiver in Android.

1. Register  the broadcastreceiver in your project’s Manifest file. If you will use this method then you can’t control the lifecycle of broadcast receiver. That means  your application will getting notification unless until you uninstall the application.

2. Register  the broadcastreceiver using Android’s Context.registerReceiver() method. By using this method, we can control it’s lifecycle by registering and un-registering broadcast receiver as per our requirement. To make this tutorial very simple, lets use Manifest process here, then we will go for Context.registerReceiver() method in a separate post.

So let’s register our own Receiver class with the incoming SMS intent filter in Manifest file. For incoming SMS example, my manifest file looks like below.

Complete Android Broadcastreceiver Example Manifest File

Your ads will be inserted here by

Easy Plugin for AdSense.

Please go to the plugin admin page to
Paste your ad code OR
Suppress this ad slot.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””
android:versionName=”1.0″ >

android:targetSdkVersion=”16″ />

<uses-permission android:name=”android.permission.RECEIVE_SMS”/>

android:theme=”@style/AppTheme” >
android:label=”@string/app_name” >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />

android:name =”.MyBroadcastReceiver”>
<action android:name=”android.provider.Telephony.SMS_RECEIVED” />



3. Add user permission for the broadcastreceiver.

Make sure to add user permissions for the broadcastreceiver. in my case (incoming SMS), i have added below permission in the above Manifest file.

<action android:name=”android.provider.Telephony.SMS_RECEIVED” />

That’s it. Once you install the application, at that time the broadcastreceiver will be automatically register with Android OS, and you will get notification as an Android Toast with details.

I hope this small tutorial on ‘Android broadcastreceiver example’ will help at it’s best.