-2

i am facing a small issue in sms in android application. I need help from you to receive the recipent address to whom i am sending message to.

sample code is given below.

public void oncall() {
        // public void onReceive(Context context, Intent intent) {
        // ---get the SMS message passed in---
        Bundle bundle = new Bundle(); // intent.getExtras();

        SmsMessage[] msgs;
        // 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]);
                String Mobno = msgs[i].getOriginatingAddress();
                // String message = msgs[i].getMessageBody().toString();
                output.setText("" + Mobno);
            }
            // ---display the new SMS message---
            // Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
        }
    }
user1239393
  • 125
  • 4
  • 10

1 Answers1

1
     Bundle bundle = intent.getExtras();
   if (bundle != null)
 {
   Object[] pdus = (Object[]) bundle.get("pdus");
   for (Object pdu : pdus)
 {
  SmsMessage smsMessage = SmsMessage.createFromPdu((byte[]) pdu);

 if(smsMessage.getOriginatingAddress() != null)
 {
String num = smsMessage.getOriginatingAddress();

 }
}
}
Pradeep Sodhi
  • 2,135
  • 1
  • 19
  • 19
  • I was wrong in my question. I apologise for that. i doubt is i am creating 2 functions in a single activity. one function is send() and another is receive() message. I need to get the mobile number to whom i sent message.. – user1239393 Mar 05 '12 at 07:21
  • The above code u implement in onReceive method of SmsReceiver class which extends BroadcastReceiver – Pradeep Sodhi Mar 05 '12 at 07:30