Skip to content Skip to sidebar Skip to footer

How To Read A Sms From Specific Mobile Number In Android

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.

Solution 1:

Code for the intent receiver that will read the SMS from intent received and show the message.

publicclassSmsReceiverextendsBroadcastReceiver{
    @Override
    publicvoid 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-permissionandroid: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.

Post a Comment for "How To Read A Sms From Specific Mobile Number In Android"