简体   繁体   中英

Android: how to get sender and receiver phone number from raw sms

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!

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();

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.

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"));



 ....

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

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();


 }                         
}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM