Listening incoming sms message in Android

By | May 1, 2011

When a new sms message is received by the device, a Broadcast Receiver is registered. For this

IntentFilter filter = new IntentFilter(SMS_RECEIVED);
registerReceiver(receiver_SMS, filter);

should be included . Also sms are sent in PDU’s(Protocol Description Units) format, which act as an encapsulation.
Inorder to extract from PDU to byte array

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

method is used.
When sms comes, the BroadCast listener is activated and sms sender number is showed in a ListView
[xml]
package com.coderzheaven.pack;

import java.util.ArrayList;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class smsReceiver extends Activity
{
/** Called when the activity is first created. */
public static final String SMS_RECEIVED = "android.provider.Telephony.SMS_RECEIVED";
ListView list;
ArrayList<String> messageList;
ArrayAdapter< String> adapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
list = (ListView) findViewById(R.id.listView1);

messageList = new ArrayList<String>();
//messageList.add("check");
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messageList);
list.setAdapter(adapter);

IntentFilter filter = new IntentFilter(SMS_RECEIVED);
registerReceiver(receiver_SMS, filter);
}
BroadcastReceiver receiver_SMS = new BroadcastReceiver()
{
public void onReceive(Context context, Intent intent)
{
if (intent.getAction().equals(SMS_RECEIVED))
{
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)
{
Toast.makeText(smsReceiver.this, "—-"+message.getDisplayMessageBody(), Toast.LENGTH_LONG).show();
receivedMessage(message.getDisplayOriginatingAddress());
}
}
}
}
};
private void receivedMessage(String message)
{
messageList.add(message);
adapter.notifyDataSetChanged();
}
}
[/xml]

The xml hold a ListView

[xml]
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ListView android:layout_height="fill_parent"
android:id="@+id/listView1"
android:layout_width="fill_parent"/>
</LinearLayout>
[/xml]

Also important part is that, for an application to listen an SMS Intent Broadcast should be added

[xml]
<uses-permission android:name="android.permission.RECEIVE_SMS"/>
[/xml]

This post explains How to get the SMS sent to your emulator within your application

6 thoughts on “Listening incoming sms message in Android

  1. Michael

    hi.. thanks for sharing an amazing code, but it not works to me.. any other solution??

    Reply
  2. Haudau

    hi.. thanks for sharing an amazing code, but it not works to me.. any other solution??

    Reply

Leave a Reply

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