Home > Tutorials > Create an application auto receiving new message (SMS)

Create an application auto receiving new message (SMS)

It’s quite a long time that I’ve been writing any new interesting tutorial. Today, I’ve got a little fever so I couldn’t step to workplace; so I decide to spend a little time to write a new tutorial.

The application we’re going to make today is a simple one that will receiving new messages automatically, notify and display them on a ListView. This is gonna be our simple screen:

SMS Auto Receiver

SMS Auto Receiver

A – Create the Project

Project Name: SMS Auto Receiver

Application Name: SMSAutoReceiver

Package Name: pete.android.study

Create Activity: MainActivity

Min SDK: 10

Click OK -> Done with creating project.

B – Sketch the Layout

The layout is pretty much the same as many previous articles on ListView in my blog.

+ One layout for main screen display, which is the list view

+ One layout for each item in the list view, which will be set to list view. This is where our SMS messages residing.

First, we start with layout for each item in list view.

1. List Item Layout

– First row determines the number of sender, I just make it into a LinearLayout with a constant TextView on the left with text “From: “ and right TextView gonna be used for setting incoming number.

– The second row displays the contents of message.

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

			android:text="From: "





2. Main Layout

– Just a ListView, no more, no less!!!

C – Class Design – On the Idea

This is what I mapped from my mind.

Clss Diagram

Class Diagram

SmsInfo class: hold information about new message, which is also implement from interface Parcelable in order to be passed through by Intent.

SmsInfoAdapter class: the adapter manages content on ListView, extends from class ArrayAdapter<SmsInfo> for simplisticity.

SmsReceiver class: extends from class BroadcastReceiver, to handle the event when new message arrived, pass these messages to Intent and pass to launch MainActivity.

MainActivity class: is a actual class receiving list of new message (List<SmsInfo>) sending from

D – From Design to Code (w/ Passion)

1. SmsInfo.java

package pete.android.study;

import android.os.Parcel;
import android.os.Parcelable;

public class SmsInfo implements Parcelable {
	private String mNumber;
	private String mContent;

	public SmsInfo(String number, String content) {
		mNumber = number;
		mContent = content;

	public SmsInfo(Parcel in) {
		String data[] = new String[2];
		mNumber = data[0];
		mContent = data[1];

	public void setNumber(String number) {
		mNumber = number;
	public String getNumber() {
		return mNumber;

	public void setContent(String content) {
		mContent = content;
	public String getContent() {
		return mContent;

	public int describeContents() {
		return 0;

	public void writeToParcel(Parcel dest, int flags) {
		dest.writeStringArray(new String[] {
			mNumber, mContent


	public static final Parcelable.Creator<SmsInfo> CREATOR = new Parcelable.Creator<SmsInfo> () {

		public SmsInfo createFromParcel(Parcel source) {

			return new SmsInfo(source);

		public SmsInfo[] newArray(int size) {

			return new SmsInfo[size];


2. SmsInfoAdapter.java

package pete.android.study;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class SmsInfoAdapter extends ArrayAdapter<SmsInfo> {

	public SmsInfoAdapter(Activity a, List<SmsInfo> list) {
		super(a, 0, list);


	public View getView(int pos, View convertView, ViewGroup parent) {

		ViewHolder holder = null;
		if(convertView == null) {
			Activity a = (Activity)getContext();
			LayoutInflater inflater = a.getLayoutInflater();
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.listitem, null);
			holder.tvNumber = (TextView)convertView.findViewById(R.id.tvNumber);
			holder.tvContent = (TextView)convertView.findViewById(R.id.tvContent);
		} else {
			holder = (ViewHolder)convertView.getTag();

		SmsInfo entry = getItem(pos);
		if(entry != null) {

		return convertView;

	static class ViewHolder {
		TextView tvNumber;
		TextView tvContent;

3. SmsReceiver.java

package pete.android.study;

import java.util.ArrayList;
import java.util.List;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;
import android.widget.Toast;
import pete.android.study.SmsInfo;

public class SmsReceiver extends BroadcastReceiver {
	static ArrayList<SmsInfo> listSms = new ArrayList<SmsInfo>();
	public void onReceive(Context context, Intent intent) {
		// get SMS map from intent
        Bundle extras = intent.getExtras();
        // a notification message
        String messages = "";
        if ( extras != null ) {
            // get array data from SMS
            Object[] smsExtra = (Object[]) extras.get( "pdus" ); // "pdus" is the key

            for ( int i = 0; i < smsExtra.length; ++i ) {
            	// get sms message
            	SmsMessage sms = SmsMessage.createFromPdu((byte[])smsExtra[i]);
            	// get content and number
            	String body = sms.getMessageBody();
            	String address = sms.getOriginatingAddress();
            	// create display message
            	messages += "SMS from " + address + " :\n";
            	messages += body + "\n";

            	// store in the list
                listSms.add(new SmsInfo(address, body));

            // better check size before continue
            if(listSms.size() > 0) {
            	// notify new arriving message
            	Toast.makeText( context, messages, Toast.LENGTH_SHORT ).show();
            	// set data to send
	            Intent data = new Intent(context, MainActivity.class);
	            // new activity

	            data.putParcelableArrayListExtra("ListSMS", listSms);
	            // start

4. MainActivity.java

package pete.android.study;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;

public class MainActivity extends Activity {
    ListView mListData;

    public void onCreate(Bundle savedInstanceState) {

        mListData = (ListView)findViewById(R.id.lvData);
        // receive list incoming messages
        ArrayList<SmsInfo> listSms = getIntent().getParcelableArrayListExtra("ListSMS");
        // check condition
        if(listSms != null && listSms.size() > 0) {
        	// set dat to list
        	SmsInfoAdapter adapter = new SmsInfoAdapter(this, listSms);

E – Additional Config

We need to set uses-permission to receive SMS and register our SMSReceiver in order to make it work.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    <uses-sdk android:minSdkVersion="10" />
	<uses-permission android:name="android.permission.RECEIVE_SMS" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".MainActivity"
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

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


F – Get the Sample Project by Pete

Click to browse Trunk from GoogleCode.


G – Final Words

– Hope you enjoy and learn something from this article 🙂

– Feel free to suggest, comment below!



Pete Houston

  1. January 5, 2018 at 2:23 pm

    Marketing92 is offering the top services of Branded SMS in Karachi. We are helping our customers to earn more profit and get more customers with our Leading services of Bulk SMS in Karachi. Now you can send Text SMS with your Branded Name to promote your Business and make you a good reputation in the market.

  2. JAO Stgo
    December 21, 2015 at 7:15 am

    Thank you so much for your code. I was researching for over a month on how to do this! It was keeping me from completing my next phase of Arduino-Android project. This was the last hurdle, figuring out the Android app part of it that can handle text messages like this. I can now interface Arduino with Android app. Thank you.

  3. January 13, 2015 at 9:35 am

    Do you need unlimited articles for your website ? I am sure you spend a lot of
    time writing articles, but you can save it for other tasks, just search in google: kelombur’s favorite tool

  4. RonaldOi
    December 27, 2013 at 2:23 am

    Hi to all members on this community i am thanks to the administrator of this forum for approve my account i am sure here i got better knowledge thanks again. My name is Ronald.

  5. November 8, 2013 at 7:05 pm

    If the activity is opened with some received msg in the list and a new sms comes a new activity starts and comes in front of the previous one. any solution to that? I just want to populate sms in the already opened activity.

    I have tried replacing FLAG_ACTIVITY_NEW_TASK
    with with different flags but i get my app crash every time.


  6. kuldeep mishra
    June 21, 2013 at 12:53 pm

    This is only receive message in listview but how to get it message from another device like chat application.

  7. ralphie
    June 21, 2013 at 8:51 am

    Thank you for this tutorial.. It helps me a lot doing my first Android app.

    One of the thing I observe is that when I click back or return in my phone, the listview also changes. The last item added will be gone in the view..
    If I add a new item.. it will append to the item that vanished when I hit the return or back button.. Is there a way to prevent this from happening?

  8. Ravi
    June 12, 2013 at 5:28 pm

    hi.i got the toaster message but i didnt get message in the listview..can anybody help me how to get

    • ralphie
      June 21, 2013 at 12:01 pm

      it works for me..

  9. vasu
    April 25, 2013 at 1:45 pm

    mListData = (ListView)findViewById(R.id.lvData);
    in this line what is lvData?

  10. wap
    March 12, 2013 at 6:11 pm

    how can it be inserted to database..?receiving text message then storing in sqlite database(in android) then query will follow when querying the received text message, date, sender will be shown.your reply is much appreciated.tnx.

  11. wap
    March 12, 2013 at 5:59 pm

    hi..with this app,how can it be inserted to sqlite database in android phone?
    particularly im trying to make an android app that can receive sms and then store it on a sqlite database then quering will follow..in quering the received text message(sms), date, sender should appear in the database..android api 10 will be used.your help is much appreciated..tnx.

  12. February 5, 2013 at 2:38 pm

    Nice Thread Tutorial, i am new in android , its help me a lot …

    I have got some good links

    here at androidexample.com
    Incomming SMS Broadcast Receiver

  13. October 24, 2012 at 6:17 am

    i have a plan, sms message is coordinaat position with getting from GPS and transferring into google map,, how do this? because google map does’nt have activity but mapactivity…please tell me way..thx

  14. October 24, 2012 at 6:15 am

    i have a plan, sms message is coordinaat position with getting from GPS and transferring into google map,, how do this? because google map does’nt have activity but mapactivity…please tell me..thx

  15. October 24, 2012 at 6:14 am

    i have a plan, sms message is coordinaat position with getting from GPS and transferring into google map,, how do this? because google map does’nt have activity but mapactivity…please tell me..thx

  16. kin
    August 7, 2012 at 4:10 pm

    hello.. i hv a problems with the lvdata in the file R.java. I try to clear my project & let it auto generated in R.java. however it doesn’t happens. Is there any solution can suggest?

    mListData = (ListView)findViewById(R.id.lvData);

    between my i know what is the lvData for?

  17. joe
    August 5, 2012 at 9:53 pm

    thanks great tutorial..but i wan t ask you.
    how to make the content of message CLICKABLE ? and there is the Link to click ..

  18. Poovizhirajan.N
    June 16, 2012 at 7:20 am

    superv example…. It s very useful…to my first project…

  19. pankaj gupta
    April 26, 2012 at 7:20 pm

    nice tutorial using parcels.can u help me to create a application that send auto message to sender, i will be very thank full.

    Thanks a lot

  20. March 22, 2012 at 12:48 pm

    time pass software

  21. sulabh
    March 14, 2012 at 1:38 pm

    sorry friend i got my sms on list view…. Superb Example…

  22. sulabh
    March 14, 2012 at 1:21 pm

    using this code i count see receive sms… Help to solve it…

  23. Meerkhalid
    March 7, 2012 at 3:43 pm


  24. jim
    February 17, 2012 at 1:33 pm

    nice tutorial using parcels. Why couldn’t you just use extras to pass the sms message to the

    • February 24, 2012 at 10:03 am

      It’s the same; however, I create a custom object structure, SmsInfo; so Parcelable is preferrable.

  25. November 21, 2011 at 5:36 am


    Does this app have to use minSDK = 10?
    I mean, can this code for receiving SMS work on older Android versions than 2.3.3, because I am going to make an application like this, but I would like it to work on Android 1.6, which uses API 4 or 5…

    • February 24, 2012 at 10:04 am

      You can set to set to lower API to see if there’s any change like: deprecated, removed …

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: