问题
I've built an sms app wich support all features for sms activities(only sms). But now, my problem is that when my app is already the default one for sms, I can't get the number from contact view when I'd like to send sms to it through my app. Here are images to explain what I'd like to achieve.! When I click to the sms icon,my app is opened by I can't get the number I do not have any code to handle the action SEND/SENDTO in my activity, but I just mentioned the intent-filter:action.SEND, action.SENDTO in the manifest file, as it is obligatory if we want to make the app selectable as the default sms app. I thought that the number from the contact view is accessed from the onActivityResult but it seems to not work, please help!
回答1:
When getting a SENDTO
from Contacts, the number - possibly multiple numbers - will be attached as the data Uri
on the Intent
that started your Activity
. When initializing your Activity
, check for the appropriate action, and retrieve the number(s) if necessary.
For a basic example:
if (Intent.ACTION_SENDTO.equals(getIntent().getAction())) {
Uri data = getIntent().getData();
String numbers = data.getSchemeSpecificPart();
}
For a more solid implementation, it would be prudent to strip any additional parameters that might be on the Uri
, and to replace any non-Latin digits.
if (Intent.ACTION_SENDTO.equals(getIntent().getAction())) {
Uri data = getIntent().getData();
String numbers = data.getSchemeSpecificPart();
// Strip any extraneous parameters
int i = numbers.indexOf('?');
numbers = (i == -1) ? numbers : numbers.substring(0, i);
// Replace non-Latin digits, and ensure our delimiter is something we expect
numbers = PhoneNumberUtils.replaceUnicodeDigits(numbers).replace(",", ";");
...
}
If multiple numbers are received, they should come in a comma- or semicolon-delimited String
. The above replaces commas with semicolons, so we don't need to worry later about which was used. You can then simply split()
the numbers
to get the individual numbers, should you've received more than one.
来源:https://stackoverflow.com/questions/42676866/from-a-contact-view-how-to-send-a-contact-number-to-my-sms-app-to-send-an-sms