本文整理汇总了Java中com.google.i18n.phonenumbers.PhoneNumberUtil类的典型用法代码示例。如果您正苦于以下问题:Java PhoneNumberUtil类的具体用法?Java PhoneNumberUtil怎么用?Java PhoneNumberUtil使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
PhoneNumberUtil类属于com.google.i18n.phonenumbers包,在下文中一共展示了PhoneNumberUtil类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendOTPNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
private void sendOTPNumber(){
if(checkNull()){
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
// assuming you only a button in your layout...
boolean isAuthentic = false;
try {
PhoneNumber number = util.parse(countryPrefix + edtMobile.getText().toString().trim(), countryIso);
isAuthentic = true;
} catch (NumberParseException e) {
e.printStackTrace();
}
if (isAuthentic) {
comman.hideSoftKeyBoard(context, edtMobile);
createJson(edtMobile.getText().toString().trim(), countryPrefix, countryName);
}
}
}
示例2: toInternationalFormat
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
public static String toInternationalFormat(String number) throws NumberParseException {
if (!isValidNumber(number)) {
throw new NumberParseException(ErrorType.NOT_A_NUMBER, number + " is not a valid number");
}
number = number.replaceAll("\\s+", "");
if (!number.contains("+")) {
number = "+" + number;
}
PhoneNumber phoneNmber = PhoneNumberUtil.getInstance().parse(number, null);
String nationalNumber = String.valueOf(phoneNmber.getNationalNumber());
String countryCode = String.valueOf(phoneNmber.getCountryCode());
String leadingZero = phoneNmber.hasItalianLeadingZero() ? "0" : "";
return countryCode + leadingZero + nationalNumber;
}
示例3: isShortCode
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
public static boolean isShortCode(@NonNull String localNumber, @NonNull String number) {
try {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber localNumberObject = util.parse(localNumber, null);
String localCountryCode = util.getRegionCodeForNumber(localNumberObject);
String bareNumber = number.replaceAll("[^0-9+]", "");
// libphonenumber doesn't seem to be correct for Germany and Finland
if (bareNumber.length() <= 6 && ("DE".equals(localCountryCode) || "FI".equals(localCountryCode) || "SK".equals(localCountryCode))) {
return true;
}
// libphonenumber seems incorrect for Russia and a few other countries with 4 digit short codes.
if (bareNumber.length() <= 4 && !SHORT_COUNTRIES.contains(localCountryCode)) {
return true;
}
Phonenumber.PhoneNumber shortCode = util.parse(number, localCountryCode);
return ShortNumberInfo.getInstance().isPossibleShortNumberForRegion(shortCode, localCountryCode);
} catch (NumberParseException e) {
Log.w(TAG, e);
return false;
}
}
示例4: loadInBackground
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
@Override
public ArrayList<Map<String, String>> loadInBackground() {
Set<String> regions = PhoneNumberUtil.getInstance().getSupportedRegions();
ArrayList<Map<String, String>> results = new ArrayList<Map<String, String>>(regions.size());
for (String region : regions) {
Map<String, String> data = new HashMap<String, String>(2);
data.put("country_name", PhoneNumberFormatter.getRegionDisplayName(region));
data.put("country_code", "+" +PhoneNumberUtil.getInstance().getCountryCodeForRegion(region));
results.add(data);
}
Collections.sort(results, new RegionComparator());
return results;
}
示例5: initializeNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
private void initializeNumber() {
PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance();
String localNumber = Util.getDeviceE164Number(this);
try {
if (!TextUtils.isEmpty(localNumber)) {
Phonenumber.PhoneNumber localNumberObject = numberUtil.parse(localNumber, null);
if (localNumberObject != null) {
this.countryCode.setText(String.valueOf(localNumberObject.getCountryCode()));
this.number.setText(String.valueOf(localNumberObject.getNationalNumber()));
}
} else {
String simCountryIso = Util.getSimCountryIso(this);
if (!TextUtils.isEmpty(simCountryIso)) {
this.countryCode.setText(numberUtil.getCountryCodeForRegion(simCountryIso)+"");
}
}
} catch (NumberParseException npe) {
Log.w(TAG, npe);
}
}
示例6: afterTextChanged
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
@Override
public void afterTextChanged(Editable s) {
if (TextUtils.isEmpty(s)) {
setCountryDisplay(getString(R.string.RegistrationActivity_select_your_country));
countryFormatter = null;
return;
}
int countryCode = Integer.parseInt(s.toString());
String regionCode = PhoneNumberUtil.getInstance().getRegionCodeForCountryCode(countryCode);
setCountryFormatter(countryCode);
setCountryDisplay(PhoneNumberFormatter.getRegionDisplayName(regionCode));
if (!TextUtils.isEmpty(regionCode) && !regionCode.equals("ZZ")) {
number.requestFocus();
}
}
示例7: formatE164
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
public static String formatE164(String countryCode, String number) {
try {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
int parsedCountryCode = Integer.parseInt(countryCode);
PhoneNumber parsedNumber = util.parse(number,
util.getRegionCodeForCountryCode(parsedCountryCode));
return util.format(parsedNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
} catch (NumberParseException | NumberFormatException npe) {
Log.w(TAG, npe);
}
return "+" +
countryCode.replaceAll("[^0-9]", "").replaceAll("^0*", "") +
number.replaceAll("[^0-9]", "");
}
示例8: updateContactName
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
private void updateContactName(String phone) {
mPhoneContactNumber = phone;
mPhoneContactName = phone;
try {
// phone must begin with '+'
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber numberProto = phoneUtil.parse(phone, "");
String countryCode = "+" + String.valueOf(numberProto.getCountryCode());
String formatedPhoneNumber = phoneUtil.format(numberProto, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
Contact contact = AppManager.getInstance().getPhoneContactFromContacts(phoneContacts, phone, countryCode);
if (contact == null) {
mPhoneContactName = formatedPhoneNumber;
} else {
mPhoneContactName = contact.getName();
}
} catch (NumberParseException e) {
System.err.println("NumberParseException was thrown: " + e.toString());
}
tvUsername.setText(mPhoneContactName);
}
示例9: toMsisdn
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
private void toMsisdn(BackendAuthResult result, String phoneNumber) {
if (StringUtils.isBlank(phoneNumber)) {
return;
}
try {
String number = phoneUtil.format(
phoneUtil.parse(
phoneNumber,
null // No default region
),
PhoneNumberUtil.PhoneNumberFormat.E164
).substring(1); // We want without the leading +
result.withThreePid(new ThreePid(ThreePidMedium.PhoneNumber.getId(), number));
} catch (NumberParseException e) {
log.warn("Invalid phone number: {}", phoneNumber);
}
}
示例10: validatePhone
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
public static boolean validatePhone(String phone, String countryCode) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
if (!ProjectUtil.isStringNullOREmpty(countryCode) && (countryCode.charAt(0) != '+')) {
countryCode = "+" + countryCode;
}
try {
if (isStringNullOREmpty(countryCode)) {
countryCode = PropertiesCache.getInstance().getProperty("sunbird_default_country_code");
}
phone = countryCode + "-" + phone;
PhoneNumber numberProto = phoneUtil.parse(phone, "");
// phoneUtil.isValidNumber(number)
ProjectLogger.log("Number is of region - " + numberProto.getCountryCode() + " "
+ phoneUtil.getRegionCodeForNumber(numberProto));
ProjectLogger.log("Is the input number valid - "
+ (phoneUtil.isValidNumber(numberProto) == true ? "Yes" : "No"));
return phoneUtil.isValidNumber(numberProto);
} catch (NumberParseException e) {
ProjectLogger.log("Exception occurred while validating phone number : ", e);
ProjectLogger.log(phone + "this phone no. is not a valid one.");
}
return false;
}
示例11: convertPhoneNumberToE164
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
public static String convertPhoneNumberToE164(Context context, String ph_no) {
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
PhoneNumber pn = null;
String temp_ph_no = null;
try {
pn = phoneNumberUtil.parse(ph_no, "");
temp_ph_no = phoneNumberUtil.format(pn, PhoneNumberFormat.E164);
} catch (Exception e) {
try {
pn = phoneNumberUtil.parse(ph_no,
((TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE))
.getSimCountryIso().toUpperCase());
temp_ph_no = phoneNumberUtil.format(pn, PhoneNumberFormat.E164);
} catch (Exception ee) {
FirebaseCrash.log(ee.getStackTrace().toString());
}
}
return temp_ph_no;
}
示例12: sendMessage
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
@Override
public SMSPersistentMessage sendMessage(Phonenumber.PhoneNumber phoneNumber, String messageText) {
final SMSPersistentMessage message = new SMSPersistentMessage();
message.setStatus(SMSMessageStatus.PENDING, null);
message.setDirection(SMSPersistentMessage.Direction.OUT);
message.setNumberTo(PhoneNumberUtil.getInstance().format(
phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164));
message.setMessage(messageText);
try {
sendMessage(message);
} catch (Exception ex) {
LOG.error("SMS send to number " + message.getNumberTo() + " has failed", ex);
message.setErrorStatus(ex);
}
return message;
}
示例13: getNormalizedAddress
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
/**
* Formats address to normal form like +[country][prefix][localnumber]
* @param address
* @return
*/
private static String getNormalizedAddress(String address) {
String number=stripAddress(address);
try {
//parse number using current locale rules
Phonenumber.PhoneNumber phonenumber=phoneNumberUtil.parseAndKeepRawInput(number, Locale.getDefault().getCountry());
//Phonenumber.PhoneNumber phonenumber = phoneNumberUtil.parse(number, Locale.getDefault().getCountry());
//format to international form
//phoneNumberUtil
number=phoneNumberUtil.format(phonenumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
//stripe any formatting symbols
return stripAddress(number);
}
catch(Exception ex) {
//in case of fail use default formatting rules
number=PhoneNumberUtils.formatNumber(number);
//stripe any formatting symbols
return stripAddress(number);
}
}
示例14: compareDefault
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
public boolean compareDefault(String address) {
try {
Phonenumber.PhoneNumber phoneNumber=phoneNumberUtil.parse(address, Locale.getDefault().getCountry());
PhoneNumberUtil.MatchType matchType=phoneNumberUtil.isNumberMatch(phoneNumber, this.getRawAddress());
if(matchType == PhoneNumberUtil.MatchType.EXACT_MATCH ||
matchType== PhoneNumberUtil.MatchType.NSN_MATCH ||
matchType == PhoneNumberUtil.MatchType.SHORT_NSN_MATCH)
return true;
matchType=phoneNumberUtil.isNumberMatch(phoneNumber, this.getNormalAddress());
if(matchType == PhoneNumberUtil.MatchType.EXACT_MATCH ||
matchType== PhoneNumberUtil.MatchType.NSN_MATCH ||
matchType == PhoneNumberUtil.MatchType.SHORT_NSN_MATCH)
return true;
return false;
}
catch(Exception e) {
Log.w(TAG, "Error comparing addresses='"+this.rawAddress+"' and '"+address+"'", e);
return false;
}
}
示例15: isValidMobileNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入依赖的package包/类
public Boolean isValidMobileNumber(String mobileNumber, String countryISO) {
Boolean isValidNumber = false;
try {
Phonenumber.PhoneNumber mNumber = phoneNumberUtil.parse(mobileNumber, countryISO);
if ("MX".equalsIgnoreCase(countryISO)) { // Temporary hardcode for mexico due to libphonenumber issue
isValidNumber = phoneNumberUtil.isValidNumber(mNumber);
} else {
isValidNumber = phoneNumberUtil.isValidNumber(mNumber) &&
( PhoneNumberUtil.PhoneNumberType.MOBILE.equals(phoneNumberUtil.getNumberType(mNumber)) ||
PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE.equals(phoneNumberUtil.getNumberType(mNumber))
);
}
} catch (NumberParseException ex) {
myLog.debug(ex.getMessage(), ex);
}
return isValidNumber;
}