Listening for Incoming SMS Messages

When a new SMS message is received by the device, a new broadcast Intent is fired with the android.provider.Telephony.SMS_RECEIVED action. Note that this is a string literal, the SDK currently doesn't include a reference to this string, so you must specify it explicitly when using it in your applications.

The SMS received action string is hidden (therefore unsupported). As such it is subject to change at any future platform release. As always, be very cautious when using unsupported platform features as they are subject to change in future platform releases.

For an application to listen for SMS Intent broadcasts, it needs to specify the receive_sms manifest permission. Request this permission by adding a <uses-permission> tag to the application manifest, as shown in the following snippet:

<uses-permission android:name="android.permission.RECEIVE_SMS"

The SMS broadcast Intent includes the incoming SMS details. To extract the array of SmsMessage objects packaged within the SMS broadcast Intent bundle, use the pdu extras key to extract an array of SMS PDUs (protocol description units — used to encapsulate an SMS message and its metadata), each of which represents an SMS message. To convert each PDU byte array into an SMS Message object, call SmsMessage.createFromPdu, passing in each byte array as shown in Listing 12-19.

LISTING 12-19: Extracting SMS messages from Incoming SMS Intent broadcasts

Available for Bundle bundle = intent.getExtras(); download on , _ _ . .

Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++)

messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

Each SmsMessage contains the SMS message details, including the originating address (phone number), time stamp, and the message body.

Listing 12-20 shows a Broadcast Receiver implementation whose onReceive handler checks incoming SMS texts that start with the string ©echo, and then sends the same text back to the number that sent it.

LISTING 12-20: Listening for incoming SMS messages

Available for public class IncomingSMSReceiver extends BroadcastReceiver { Wrox°comn private static final String queryString = "@echo";

private static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";

public void onReceive(Context _context, Intent _intent) { if (_intent.getAction().equals(SMS_RECEIVED)) {

SmsManager sms = SmsManager.getDefault();

Bundle bundle = _intent.getExtras(); if (bundle != null) {

Object[] pdus = (Object[]) bundle.get("pdus"); SmsMessage[] messages = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++)

messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);

for (SmsMessage message : messages) { String msg = message.getMessageBody();

String to = message.getOriginatingAddress();

if (msg.toLowerCase().startsWith(queryString)) { String out = msg.substring(queryString.length()); sms.sendTextMessage(to, null, out, null, null);

To listen for incoming messages, register the Broadcast Receiver from Listing 12-20 using an Intent Filter that listens for the android.provider.Telephony.SMS_RECEIVED action String, as shown in Listing 12-21.

LISTING 12-21: Registering an SMS listener receiver final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED"; IntentFilter filter = new IntentFilter(SMS_RECEIVED);

BroadcastReceiver receiver = new IncomingSMSReceiver(); registerReceiver(receiver, filter);

Was this article helpful?

0 0
Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


  • Primula
    How to receive a broadcast sms_received in string?
    8 years ago
  • tuulia
    How to incoming pdu sms?
    8 years ago
  • Topias
    How to convert the sms pdu to a byte array?
    5 years ago

Post a comment