Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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!

like image 526
User12111111 Avatar asked Oct 18 '13 13:10

User12111111


3 Answers

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.

like image 170
mesh Avatar answered Oct 24 '22 11:10

mesh


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();
like image 38
Naveed Ahmad Avatar answered Oct 24 '22 12:10

Naveed Ahmad


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



 ....
like image 1
Will R. Avatar answered Oct 24 '22 11:10

Will R.