It is pretty easy to play with received sms on Android, i will go step by step with code example of project.
step 1:
Create a android project and add one more class in project, let's assume for this example "MySMSReceiver.java", and extends this class with
"BroadcastReceiver" there you will get one method "
onReceive()", this method will active when you will receive any sms in this case, so you have to over-ride this method.
package imran.smsreceiver;
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 MySMSReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
//---get the SMS message passed in the intent,---
Bundle _bundle = intent.getExtras();
Toast.makeText(context, "A message came", Toast.LENGTH_SHORT).show();
SmsMessage[] _msg = null;
String str = "";
if (_bundle != null)
{
//---retrieve the SMS message received by pdus---
Object[] _pdus = (Object[]) _bundle.get("pdus");
_msg = new SmsMessage[_pdus.length];
for (int i=0; i<_msg.length; i++){
_msg[i] = SmsMessage.createFromPdu((byte[])_pdus[i]);
//---to get address of sender---
str += "SMS from " + _msg[i].getOriginatingAddress();
str += " :";
//---to find message of sender---
str += _msg[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message with sender information.---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}//end of class
The above code will describe you how to get sms sender information as well as sms body.
step 2:
In Manifest.xml you have to add
<receiver>
so that MySMSReceiver class can be active when sms will come on you device.below code of Manifest.xml will describe how to add
<receiver>.