Get the recipient number with SMS on Android

It may seem strange, but I want to get to which number I received the SMS on my phone. I can get a mobile phone number for the SIM card inside telephonyManager.getLine1Number()(although it is not guaranteed that I will get a phone number). But what if I have two SIM cards in my phone. Since it telephonyManager.getLine1Number()returns only SIM card information only for the first / active SIM card.

Edited . I also tried to get all the information I receive whenever I receive an SMS, but I could not find anything interesting.

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

+5
source share
1 answer

Try this code,

 public class SmsReceiver extends BroadcastReceiver {    

@Override public void onReceive ( , ) {

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. }

}

-1

All Articles