Developing Android Broadcast Receiver PRO

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";

	@Override
	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="http://schemas.android.com/apk/res/android"
	package="com.abelski.samples" android:versionCode="1"
	android:versionName="1.0">
	<application android:icon="@drawable/icon" android:label="@string/app_name">
		<activity android:name=".SMSReceiverActivity" 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=".MySMSReceiver">
			<intent-filter>
				<action android:name="android.provider.Telephony.SMS_RECEIVED" />
			</intent-filter>
		</receiver>

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

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

Share:

The Visitor Design Pattern

The Visitor Design Pattern

The visitor design pattern allows us to add operations to objects that already exist without modifying their classes and without extending them.

What are Anti Patterns?

Anti Patterns

Unlike design patterns, anti patterns just seem to be a solution. However, they are not a solution and they cause additional costs.

Virtual Threads in Java Professional Seminar

Virtual Threads in Java

The use of virtual threads can assist us with improving the performance of our code. Learn how to use virtual threads effectively.

NoSQL Databases Courses, Seminars, Consulting, and Development

MongoDB Design Patterns Meetup

The use of MongoDB involves with various cases in which we can overcome performance issues by implementing specific design patterns.

image of woman and database

Record Classes in Java

Learn how to define record classes in Java, and when to use record classes in your code. Stay up to date with the new Java features.

Accessibility | Career | Conferences | Design Patterns | JavaScript | Meetups | PHP | Podcasts | Python | Self Learning

Teaching Methodologies | Fullstack | C++ | C# | CSS | Node.js | Angular | Java | Go | Android | Kotlin | Swift | Academy

Front End Development | Scala | Architectures | Cloud | Big Data | Internet of Things | Kids Learn Programming

The Beauty of Code

Coding is Art! Developing Code That Works is Simple. Develop Code with Style is a Challenge!

Skip to content Update cookies preferences