Friday 6 September 2013


Before Receiving Message

Receive,Read(sender number/name,message) SMS

After Receiving Message

Receive,Read(sender number/name,message) SMS

//my network provider send me a SMS that you do not have balance :)

1. code :
package com.shaikhhamadali.blogspot.textmessage;

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

public class SMSReceiver extends BroadcastReceiver
{
    public void onReceive(Context context, Intent intent)
    {
     
        Bundle myBundle = intent.getExtras();
        SmsMessage [] messages = null;
        String strMessage = "";
        
        if (myBundle != null)
        {
         //get message in pdus format(protocol discription unit)
            Object [] pdus = (Object[]) myBundle.get("pdus");
            //create an array of messages
            messages = new SmsMessage[pdus.length];

            for (int i = 0; i < messages.length; i++)
            {
             //Create an SmsMessage from a raw PDU. 
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
                //get the originating address (sender) of this SMS message in String form or null if unavailable 
                strMessage += "SMS From: " + messages[i].getOriginatingAddress();
                strMessage += " : ";
                //get the message body as a String, if it exists and is text based.
                strMessage += messages[i].getMessageBody();
                strMessage += "\n";
            }
            //show message in a Toast
            Toast.makeText(context, strMessage, Toast.LENGTH_SHORT).show();
        }
    }
}


2. code of manifest:
add this in your manifest file:
 <uses-permission android:name="android.permission.RECEIVE_SMS"/>

       <receiver android:name=".SMSReceiver" >
            <intent-filter>
                <action android:name="android.provider.Telephony.SMS_RECEIVED" />
            </intent-filter>
        </receiver>

3. note that:
  • this post is the part two of my previous post Send and track SMS.
  • add this post contents (permissions and SMSReceiver broadcast)to previous code.
  • do not forget to provide permission in manifest file either you can not receive the message or can get the exception.
  • you can use this with database and can create messege sender and receiver application.
  • BroadCast Receivers,SmsManager,Toasts,Logs and types of logs.
  • more about PDU.
8. conclusion:
  • Some information about PDU,Broad Cast Receivers and permissions.
  • Know how to send track and receive an sms.
9. about the post:
  •  The code seems to explain itself due to comments, and is very easy to understand.
  •  Don’t mind to write a comment whatever you like to ask, to know,to suggest or recommend.
  •  Hope you enjoy it!

10. Source Code:
        you can download the source code here

Cheers,
Hamad Ali Shaikh