Search code examples
androidandroid-intentbroadcastreceiversms

Get SMS sender name who is NOT in contact list?


I'm trying to get the name of the sender of the SMS, who is not in the user's contact list. Sometimes, we get SMS from some corporations/banks etc, and we can see the name of that corporation/bank as header in the item of SMS list in our SMS box, instead of their number, even if we have not saved it to our contact list. Is it possible to get that name through intent obtained from broadcast receiver? Right now I'm getting 'pdu' from the SMS intent, and creating SMSMessage object from it as:

                    @Override
                    public void onReceive(Context context, Intent intent) {
                    Bundle data = intent.getExtras();
                    Object[] pdus = (Object[]) data.get("pdus");
                    String recMsgString = "";
    
                    for(int i=0; i<pdus.length; i++){
                        SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdus[i]);
                        byte[] userData = smsMessage.getUserData();
                        if (userData!=null){
                            for(int index=0; index<userData.length; ++index)
                            {
                                recMsgString += Character.toString((char)userData[index]);
                            }
                        }
                                 message =
                                 "Service center address: " + smsMessage.getServiceCenterAddress()+
                                         "\nPseudo object: " + smsMessage.getPseudoSubject() +
                                 "\nDisplay originating address : " + 
 smsMessage.getDisplayOriginatingAddress()
                                + "\nOriginating address : " + smsMessage.getOriginatingAddress()
                                +"\nUser data : " + recMsgString
                                + "\nEmail From: " + smsMessage.getEmailFrom()
                                + "\nEmail Body: " + smsMessage.getEmailBody()
                                + "\nDisplay message body: " + smsMessage.getDisplayMessageBody()
                                + "\nTime in millisecond: " + smsMessage.getTimestampMillis()
                                + "\nMessage: " + smsMessage.getMessageBody();
    
                    }

But the SMSMessage class doesn't seem to have any method to enable us to get the name of the sender.

How do we do it? Thanks a lot!


Solution

  • It is possible to send an SMS and provide an alphanumeric string as "sender" instead of the sender's phone number. That may be what you are seeing. You should be able to get this alphanumeric string by using getOriginatingAddress().