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

I have a problem finding the recipient's phone number from incoming raw SMS. Here is the code I'm trying to do:

Can someone tell me how to get the recipient's phone number 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 the help in advance!

+6
source share
5 answers

TelephonyManager is not suitable for the solution. Because in some cases, the number is not saved on the SIM card. Due to my suggestion, you should use the Shared Preference to store the user number for the first time the application is open, and after that the number will be used whenever you need the application.

If you want to get the sender number, you only call this method on onReceive of Broadcast when you initialize the bundle to receive the contents of the message.

 messsage[0].getOriginatingAddress(); 
+3
source

SMS is delivered by MAP, which is the protocol between the SIM card and MSC / GMSC / SMSC (some details here ). A SIM card is already identified in this association. In addition, SMS DELIVERY does not include the address of the recipient. See this . So, as far as I understand, there is no direct API for what you wanted, possibly due to the above reasons.

+3
source

use 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")); .... 
+1
source

Try the following:

 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.

0
source
 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(); } } } 
-1
source

Source: https://habr.com/ru/post/956247/


All Articles