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!
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.
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();
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"));
....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With