Home » Android » Android: how to get phone number from a incoming SMS?

Android: how to get phone number from a incoming SMS?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I have a broadcast receiver SMSApp which onReceive reads bundleExtras from intent. Here, How can I read phone number from bundleExtras?

if(intent.getAction().equals(SMS_RECEIVED))
{
  Bundle bundleExtras = intent.getExtras();
  if (bundleExtras != null) 
    {

    }
}
How to&Answers:
Bundle bundle = intent.getExtras();
            Object[] pdus = (Object[]) bundle.get("pdus");
            final SmsMessage[] messages = new SmsMessage[pdus.length];
            for (int i = 0; i < pdus.length; i++) {
                messages[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
            }

Now, messages[0].getOriginatingAddress() is the address and messages[0].getMessageBody() is the message body.

Answer:

Tutorial at http://ukitech.blogspot.com/2014/11/android-sms-app.html

OUTPUT:

SmsReceiver.processReceivedSms﹕ SMS from +1650815xxxx

SmsReceiver.processReceivedSms﹕ SMS body Test 4

/**
* Created by uki on 11/22/14.
*/

public class SmsReceiver extends BroadcastReceiver {

@Override
      public void onReceive(Context context, Intent intent) {
            final String tag = TAG + ".onReceive";
            Bundle bundle = intent.getExtras();
            if (bundle == null) {
                  Log.w(tag, "BroadcastReceiver failed, no intent data to process.");
                  return;
            }
            if (intent.getAction().equals(SMS_RECEIVED)) {
                  Log.d(tag, "SMS_RECEIVED");

                  String smsOriginatingAddress, smsDisplayMessage;

                  // You have to CHOOSE which code snippet to use NEW (KitKat+), or legacy
                  // Please comment out the for{} you don't want to use.

                  // API level 19 (KitKat 4.4) getMessagesFromIntent
                  for (SmsMessage message : Telephony.Sms.Intents.
                                            getMessagesFromIntent(intent)) {
                        Log.d(tag, "KitKat or newer");
                        if (message == null) {
                              Log.e(tag, "SMS message is null -- ABORT");
                              break;
                        }
                        smsOriginatingAddress = message.getDisplayOriginatingAddress();
                        //see getMessageBody();
                        smsDisplayMessage = message.getDisplayMessageBody();
                        processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
                  }

                  // Processing SMS messages the OLD way, before KitKat,
                  // this WILL work on KitKat or newer Android
                  // PDU is a “protocol data unit”, which is the industry
                  // format for an SMS message
                  Object[] data = (Object[]) bundle.get("pdus");
                  for (Object pdu : data) {
                        Log.d(tag, "legacy SMS implementation (before KitKat)");
                        SmsMessage message = SmsMessage.createFromPdu((byte[]) pdu);
                        if (message == null) {
                              Log.e(tag, "SMS message is null -- ABORT");
                              break;
                        }
                        smsOriginatingAddress = message.getDisplayOriginatingAddress();
                        // see getMessageBody();
                        smsDisplayMessage = message.getDisplayMessageBody();
                        processReceivedSms(smsOriginatingAddress, smsDisplayMessage);
                  }
            } // onReceive method

Answer:

This is a link to a detailed “how to send and receive sms messages”, you should check it, it is complete and full with examples:

http://mobiforge.com/developing/story/sms-messaging-android

also using this link, you can do:

if (bundle != null) {
    SmsMessage[] messages = Telephony.Sms.Intents.getMessagesFromIntent(intent);
    for (int i = 0; i &lt; messages.length; i++) {
        SmsMessage message = messages[i];
        buf.append("Received SMS from  ");
        buf.append(message.getDisplayOriginatingAddress());
        buf.append(" - ");
        buf.append(message.getDisplayMessageBody());
    }
}