9

I have a problem in finding out the receiver phone number from the incoming raw SMS. Here is the code that I am trying:

Can someone tell me how to retrieve receiver phonenumber from raw SMS.

public class SMSReceiver extends BroadcastReceiver {

private Context context;

@Override
public void onReceive(Context context, Intent intent) {
    this.context = context;
    // Parse the SMS.
    Bundle bundle = intent.getExtras();
    SmsMessage[] msgs = null;
    String str = "";
    if (bundle != null)
    {
        // Retrieve the SMS.
        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]);
            //appending to str String.
             str += "OriginatingAddress: ";
            str += msgs[i].getOriginatingAddress();
            str += " :\n";
            str += " :\n";
            str += "DisplayOriginatingAddress: ";
            str += msgs[i].getDisplayOriginatingAddress();
            str += " :\n";
            str += " :\n";
            str += "DisplayMessageBody: ";
            str += msgs[i].getDisplayMessageBody();
            str += " :\n";
            str += " :\n";
            str += "MessageBody: ";
            str += msgs[i].getMessageBody();
        }
        Toast.makeText(context, str, Toast.LENGTH_SHORT).show();

    }
}

Thanks for help in advance!

User12111111
  • 1,179
  • 1
  • 19
  • 41
  • 1
    The current answers do not show how to get the number of the receiving party. I will award the bounty on an answer that can get the phone number, or show that it's not possible. – Dave Chen Aug 21 '14 at 22:58
  • @DaveChen The PDU metadata for an SMS message does not contain the address of the recipient, and therefore it cannot be retrieved from an incoming text directly. It is possible, however, to verify a device's number by having the device send itself a text. – Mike M. Aug 23 '14 at 05:30

5 Answers5

4

SMS is delivered by MAP which is a protocol between SIM and the MSC/GMSC/SMSC (Some details here). The SIM is already identified in this association. Also the SMS DELIVER does not include the recipient address. See this. So, as far as i understand there is not an direct API for what you desired maybe because of the above reasons.

mesh
  • 849
  • 9
  • 16
3

TelephonyManager is not the right Solution,Because in some cases the number is not stored in the SIM, Due to my suggestion,You should use Shared Preference to store user's Phone number first time the application is open, and after that the number will used whenever you need in application.

If you want to To get the Sender number, you only call this method in onReceive of Broadcast when you inistiaze a bundle to get message Contents

messsage[0].getOriginatingAddress();
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
1

use the content provider

 Uri uri = Uri.parse("content://sms");  
 Cursor messageCursor = cr.query(uri, new String[] { "_id", "thread_id", "address", "date", "type", "body"}, null , null, "date" + " ASC");

 messageCursor.moveToFirst();

 String address = messageCursor.getString(messageCursor.getColumnIndex("address"));
 String type = messageCursor.getString(messageCursor.getColumnIndex("type"));



 ....
Will R.
  • 110
  • 1
  • 12
  • 2
    Not a correct solution. Thanks for the reply. To be clear, the question is how to get receiver's phone number from a RAW SMS? – User12111111 Nov 18 '13 at 07:59
0

Try this:

TelephonyManager manager =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = manager.getLine1Number();  

This only works on devices where the number is stored on the SIM card

ramaral
  • 6,149
  • 4
  • 34
  • 57
  • 2
    That's not the correct solution, which I am looking for. How does a dual SIM mobiles are recognizing which SIM card got the SMS even though phone number is not saved in the SIM cards? However, I am not looking for device/SIM to tell me the phone number. This may work for fewer SIMs and not all. Can you please tell me if there is a possibility to know receiver phone number from a RAW SMS instead of depending on the device/SIM. – User12111111 Oct 21 '13 at 07:29
  • I had researched on this but I couldn't find out any. I guess, the android API doesn't support receiver's number. I may be wrong. – User12111111 Oct 22 '13 at 08:00
  • I do not know anything else that may help you – ramaral Oct 22 '13 at 12:52
  • I have multiple sim cards. How do I know which one is the receiver? – Aero Wang Jun 22 '22 at 06:42
-2
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();                     
          // here you retrieve receiver phonenumber 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();


 }                         
}
}
Vaishali Sutariya
  • 5,093
  • 30
  • 32