In order to develop an android broadcast receiver we should define a class that extends BroadcastReceiver, implement our onReceive method and update the android platform about the new broadcast receiver, either via the manifest xml file or by calling the registerReceiver method.

The following code sample includes the definition of a BroadcastReceiver that will get the Intent the android platform fires each time a SMS message arrives.

package com.abelski.samples;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;
import android.util.Log;

public class MySMSReceiver extends BroadcastReceiver
	private static final String SMS_RECEIVE_ACTION = "android.provider.Telephony.SMS_RECEIVED";

	public void onReceive(Context context, Intent intent)
		Log.i("sms_receiver","within the onReceive method");
		if (intent != null
				&& intent.getAction() != null
				&& intent.getAction().compareToIgnoreCase(SMS_RECEIVE_ACTION) == 0)
			Object[] vec = (Object[]) intent.getExtras().get("pdus");
			SmsMessage[] messages = new SmsMessage[vec.length];
			for (int i = 0; i < vec.length; i++)
				messages[i] = SmsMessage.createFromPdu((byte[]) vec[i]);
				Log.i("sms_receiver", messages[i].getMessageBody());

The android platform is updated about this broadcast receiver via the manifest xml file.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""
	package="com.abelski.samples" android:versionCode="1"
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".SMSReceiverActivity" android:label="@string/app_name">
				<action android:name="android.intent.action.MAIN" />
				<category android:name="android.intent.category.LAUNCHER" />

		<receiver android:name=".MySMSReceiver">
				<action android:name="android.provider.Telephony.SMS_RECEIVED" />

	<uses-sdk android:minSdkVersion="7" />
	<uses-permission android:name="android.permission.RECEIVE_SMS" />

The following video clip overviews this code sample, shows its execution and explains it.

Leave a Reply

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