问题
My app used SMSManager to send SMS to number which saved in Contact List
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage("phoneNo", null, "sms message", null, null);
I got feedback from user, some number in their contact list can not receive SMS from the app. Seem this issue relate with format phone number for each country.
A user from US feedback the Phone Number of this format can not receive SMS. Number : (555)444-6666
I think I should convert the phone number to a "standard" phone number before sending SMS.
What is standard format phone number should I use?
If I remove all special character of number from (555)444-6666 to 5554446666, this way is a good way to apply for all country ?
回答1:
there is no standard needed to send messages.
You can copy and paste this code to retrieve contact phone number.
private String readcontacts(Context context, String cName) {
ContentResolver cr = context.getContentResolver();
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
null, null, null);
if (cur.getCount() > 0) {
while (cur.moveToNext()) {
String id = cur.getString(cur
.getColumnIndex(ContactsContract.Contacts._ID));
String name = cur
.getString(
cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME))
.toLowerCase();
if (name.equals(cName.toLowerCase())) {
if (Integer
.parseInt(cur.getString(cur
.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
// get the phone number
Cursor pCur = cr
.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID
+ " = ?", new String[]{id},
null);
while (pCur.moveToNext()) {
String phone = pCur
.getString(pCur
.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
return phone;
}
pCur.close();
}
}
}
}
return "Nothing found for " + cName + "!";
}
You just need to specify the contact name "cName" in this method and android system will return the phone number in correct format.
and then send SMS.
public void sendSMS(String message) {
SmsManager smsManager = SmsManager.getDefault();
smsManager.sendTextMessage(readcontacts(context, "john"), null, message, null, null);
}
来源:https://stackoverflow.com/questions/38940103/what-is-standard-format-phone-number-to-send-sms