2

I need to retrieve a phone number from different type of intents. Cases are intents as:

Intent smsIntent = new Intent(android.content.Intent.ACTION_VIEW);
smsIntent.setType("vnd.android-dir/mms-sms");
smsIntent.putExtra("address","phoneNumber");         
smsIntent.putExtra("sms_body","message");
startActivity(smsIntent);

or:

Intent sms_intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:xxxxxxxxxx"); 
sms_intent.putExtra("sms_body", "This is my message!"); 
startActivity(sms_intent);

When I start the new activity, I get my bundle extras with:

Bundle extras = getIntent().getExtras();
    if (extras != null) {
        //some code
    }

Specifically, for the first case where the intent explicitly bind an extra with key "address" I do:

extra_phoneNumber = extras.getString("address");

But I'm having some trouble trying to retrieve the value after smsto: for the second intent case.

I already tried to iterate through the bundle extras with:

for (String key : bundle.keySet()) {
Object value = bundle.get(key);
Log.d(TAG, String.format("%s %s (%s)", key,  
    value.toString(), value.getClass().getName()));

}

but if the extras are empty this give me a NPE.

I couldn't find anything because of the lack of documentation from Google itself so any answer would be really helpful! Thanks in advance.

Lampione
  • 1,622
  • 3
  • 21
  • 39
  • 1
    I suggest you check how does 2nd case intent look inside via debugger. There are other fields than extras where information can be stored. See http://developer.android.com/reference/android/content/Intent.html#getData%28%29 maybe this will help – mhenryk Jul 30 '15 at 12:39
  • Perfect! .getDataString() worked! I honestly tried to call getData() before but I was calling it on the wrong object.. Please, write this as answer so that I can mark it correct in order to help others with the same problem! – Lampione Jul 30 '15 at 13:20

2 Answers2

1

I suggest you check how does 2nd case intent look inside via debugger. There are other fields than extras where information can be stored. Please check Intent.getData() or the Intent.getDataString()

mhenryk
  • 551
  • 5
  • 13
0

You can try this code :

SmsMessage[] msgs = null;
msgs = new SmsMessage[pdus.length];
for (int i = 0; i < msgs.length; i++) {
          msgs[i] = SmsMessage.createFromPdu((byte[]) pdus[i]);
          msg_from = msgs[i].getOriginatingAddress();
          String msgBody = msgs[i].getMessageBody();
          Log.d("resulty", "Message From: " + msg_from + " and it says: " + msgBody);          
}
LittleBobbyTables - Au Revoir
  • 32,008
  • 25
  • 109
  • 114
Fareed
  • 560
  • 2
  • 7
  • 23