Android SDK Tutorial: How To Receive Text Messages

In this post I will teach to make app that can receive a text message, you will be able to obtain the sending number and message to do with it as you please, in this app we will simply display the information as shown in the images below.

this app receives and displays text messages

How the app looks when no messages have been received

the sms receiving app with one message

the app after receiving one text message, the number was covered with a white box

sms app with two text messages

the application after receiving two text messages. The numbers were covered with a white box.

JAVA Activity File ReceiveSMSActivity.java

package com.example.receivesms;

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

public class ReceiveSMSActivity extends Activity{
	
	static TextView messageBox;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        messageBox=(TextView)findViewById(R.id.messageBox);
    }
    
    public static void updateMessageBox(String msg)
    {
    	messageBox.append(msg);
    }

}

JAVA Broadcasting Listener File TextMessageReceiver.java

This java class in charge of listening for new text messages and processing them.

package com.example.receivesms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsMessage;

public class TextMessageReceiver extends BroadcastReceiver{
	
	public void onReceive(Context context, Intent intent)
	{
		Bundle bundle=intent.getExtras();
		
		Object[] messages=(Object[])bundle.get("pdus");
		SmsMessage[] sms=new SmsMessage[messages.length];
		
		for(int n=0;n<messages.length;n++){
			sms[n]=SmsMessage.createFromPdu((byte[]) messages[n]);
		}
		
		for(SmsMessage msg:sms){
			ReceiveSMSActivity.updateMessageBox("\nFrom: "+msg.getOriginatingAddress()+"\n"+
					"Message: "+msg.getMessageBody()+"\n");
		}
	}
}

Layout File main.xml

The only GUI is a text view, this is where the new messages appear.

<?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:id="@+id/messageBox"
   	android:text="message will appear here"
   	android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
   />  
</LinearLayout>

Android Manifest File AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.example.receivesms"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="8" />


    <application android:icon="@drawable/icon" android:label="@string/app_name" android:debuggable="true">
      <activity android:name=".ReceiveSMSActivity"
                  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=".TextMessageReceiver"> 
            <intent-filter> 
                <action android:name="android.provider.Telephony.SMS_RECEIVED" /> 
            </intent-filter> 
        </receiver>
    </application>
    
        <uses-permission android:name="android.permission.RECEIVE_SMS" />
    
</manifest>
blog comments powered by Disqus