Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

sms

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) 
    {

    }
}
like image 999
Sushan Ghimire Avatar asked Feb 21 '12 13:02

Sushan Ghimire


2 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.

like image 176
Seshu Vinay Avatar answered Sep 20 '22 11:09

Seshu Vinay


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
like image 28
user3566154 Avatar answered Sep 22 '22 11:09

user3566154