Android Broadcastreceiver Example Code Description

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”;
@Override
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

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.techblogon.androidbroadcastrecieverexample”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”16″ />

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

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.techblogon.androidbroadcastrecieverexample.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>

<receiver
android:name =”.MyBroadcastReceiver”>
<intent-filter>
<action android:name=”android.provider.Telephony.SMS_RECEIVED” />
</intent-filter>
</receiver>

</application>

</manifest>

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.

Leave a Reply

Your email address will not be published. Required fields are marked *