Blog : Android Broadcastreceiver Example Code Description
Android Broadcastreceiver Example Code Description
May 23, 2013 Android Tutorial, Tutorials
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 BroadcastReceiver class. 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[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();
}
}
}
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 classpublic 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
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
package="com.techblogon.androidbroadcastrecieverexample"
android:versionCode="1"
android:versionName="1.0" >
android:minSdkVersion="8"
android:targetSdkVersion="16" />
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
android:name="com.techblogon.androidbroadcastrecieverexample.MainActivity"
android:label="@string/app_name" >
android:name =".MyBroadcastReceiver"
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.
1
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.