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

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)
        list = (ListView) findViewById(R.id.listView1);

        messageList  = new ArrayList<String>();
       adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, messageList);

        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();
	private void receivedMessage(String message)

The xml hold a ListView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
<ListView android:layout_height="fill_parent"

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

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

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



  1. Pharmf910 May 6, 2011 at 5:00 pm

    Hello! interesting site!

  2. iZigg 90210 June 15, 2011 at 5:20 am

    You’ve got great insights about text message, keep up the good work!

  3. Michael July 23, 2011 at 2:09 pm

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

    • James July 23, 2011 at 2:20 pm

      What do you mean by not working? what are you getting?

  4. Haudau April 15, 2014 at 3:39 am

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

  5. eunic July 11, 2015 at 6:23 am

    I can’t get the sms display in my listview


Leave a Reply

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




Theme by HermesThemes

Copyright © 2018 CoderzHeaven. All Rights Reserved

Please wait...

Subscribe to our newsletter

Want to be notified when our article is published? Enter your email address and name below to be the first to know.