问题描述
我想将设备中的每个电话号码都转换为E164格式.因此,我在下面使用了开源.
I want convert every phone number from conatct in device to E164 format.So, I used opensource below.
所以我像这里一样使用它.
So I used it like here.
Phonenumber.PhoneNumber formattedNumber = null;
String formatted = null;
try {
formattedNumber = phoneUtil.parse(phoneNumber, "KR");
formatted = phoneUtil.format(formattedNumber,PhoneNumberUtil.PhoneNumberFormat.E164);
if (StringUtils.isEmpty(formatted) == false && formatted.length() > 0 && StringUtils.isEmpty(name) == false && name.length() > 0) {
listName.add(name);
listPhoneNumber.add(formatted);
}
} catch (NumberParseException e) {
continue;
}
我读到从4.0开始,Android框架就使用了这个库.
And I read that this library is used by the Android framework since 4.0.
我想从Android SDK中使用它.所以我找到了.Android SDK提供了此 PhoneNumberUtils
I want to use this from Android SDK. So I found this. Android SDK provides this PhoneNumberUtils.
有一个功能
它真的很容易使用.但此功能的API级别为21.
It's really easy to use. but API level of this function is 21.
所以我的问题是..如何在API Level 14(ICS)〜21下使用PhoneNumberUtils将电话号码转换为E164?
So My question is..How can I use PhoneNumberUtils to convert phonenumber to E164 under API Level 14(ICS) ~ 21?
谢谢.!
推荐答案
问题是 PhoneNumberUtils.formatNumberToE164(...)
在较旧的设备上不可用,PhoneNumberUtils中没有其他功能可以一样的工作.
The problem is PhoneNumberUtils.formatNumberToE164(...)
is not available on older devices and there's nothing else in PhoneNumberUtils that does the same job.
我建议使用PhoneNumberUtils(如果可用),并在较旧的设备上使用libphonenumber,例如:
I'd suggest using PhoneNumberUtils when available and libphonenumber on older devices, for example:
public String formatE164Number(String countryCode, String phNum) {
String e164Number;
if (TextUtils.isEmpty(countryCode)) {
e164Number = phNum;
} else {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
e164Number = PhoneNumberUtils.formatNumberToE164(phNum, countryCode);
} else {
try {
PhoneNumberUtil instance = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = instance.parse(phNum, countryCode);
e164Number = instance.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
} catch (NumberParseException e) {
Log.e(TAG, "Caught: " + e.getMessage(), e);
e164Number = phNum;
}
}
}
return e164Number;
}
这篇关于在Android中将电话号码格式化为E164格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!