5

This may sound strange but I want to get to which number I've received SMS on, on my phone. I can get the mobile number for the SIM present inside with telephonyManager.getLine1Number() (although it is not guaranteed that I'll get the phone number). But what in case I have two SIMs present in my phone. As telephonyManager.getLine1Number() will only return me SIM info for first/active SIM only.

Edited : I also tried to get all the info that I get whenever I receive any SMS, but I could not find anything interesting.

So is there any way to get recipient's number (and not sender's number) from the SMS received?

Rajkiran
  • 15,845
  • 24
  • 74
  • 114

1 Answers1

-2

Try this code,


public class SmsReceiver extends BroadcastReceiver {    

@Override public void onReceive(Context context, Intent intent) {

Bundle extras = intent.getExtras(); if (extras == null) return; Object[] pdus = (Object[]) extras.get("pdus"); String[] fromAddress = new String[pdus.length]; SmsMessage[] message = new SmsMessage[pdus.length]; for (int i = 0; i < pdus.length; i++) { message[i] = SmsMessage.createFromPdu((byte[])pdus[i]); fromAddress[i] = message[i].getOriginatingAddress(); // it will give the address from where SMS was originated. }

}

RKS
  • 445
  • 4
  • 14