Possible Duplicate:
Read all SMS from a particular sender
I want to know how to read sms and how split mobile number and message body. Please give me a sample code.
Possible Duplicate:
Read all SMS from a particular sender
I want to know how to read sms and how split mobile number and message body. Please give me a sample code.
Code for the intent receiver that will read the SMS from intent received and show the message.
public class SmsReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str += "SMS from " + msgs[i].getOriginatingAddress();
str += " :";
str += msgs[i].getMessageBody().toString();
str += "\n";
}
//---display the new SMS message---
Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
}
}
}
And make sure to add this permission in your manifest file.
<uses-permission android:name="android.permission.RECEIVE_SMS">
</uses-permission>
Also, msgs[i].getOriginatingAddress()
gives you the sender of the SMS and you can check if this is your specific number or not. And then use msgs[i].getMessageBody().toString();
to show the body of the SMS.
This tutorial covers some of the aspects of your question.
Hope it helps.