androidandroid-intentextrasandroid-bundle

Get sms address from intent data


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.


Solution

  • 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()