本文整理汇总了Java中com.google.i18n.phonenumbers.PhoneNumberUtil.isValidNumber方法的典型用法代码示例。如果您正苦于以下问题:Java PhoneNumberUtil.isValidNumber方法的具体用法?Java PhoneNumberUtil.isValidNumber怎么用?Java PhoneNumberUtil.isValidNumber使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.i18n.phonenumbers.PhoneNumberUtil
的用法示例。
在下文中一共展示了PhoneNumberUtil.isValidNumber方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: convertToE164
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
/**
* Helper method to convert a phone number to E164 format as expected by the server
* (+1XXXXXXXXXX)
*
* @param raw_phone_number The unformatted phone number as stored by the system contacts
* application
* @return String storing the E164-formatted phone number
*/
public static String convertToE164(String raw_phone_number) {
PhoneNumberUtil phone_util = PhoneNumberUtil.getInstance();
try {
Phonenumber.PhoneNumber phone_proto = phone_util.parse(raw_phone_number,
Constants.DEFAULT_PHONE_REGION);
if (phone_util.isValidNumber(phone_proto)) {
return phone_util.format(phone_proto, PhoneNumberUtil.PhoneNumberFormat.E164);
} else {
Log.d(TAG, "Invalid number");
return null;
}
} catch (NumberParseException e) {
Log.d(TAG, "Error parsing phone number");
return null;
}
}
示例3: checkPhoneNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
static void checkPhoneNumber(String phoneNumberStr)
throws NumberParseException, IllegalArgumentException {
if (phoneNumberStr == null || phoneNumberStr.length() == 0) {
throw new IllegalArgumentException("Phone number is empty");
} else if (phoneNumberStr.charAt(0) != '+') {
throw new IllegalArgumentException("Phone number should start with '+'");
}
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneUtil.parse(phoneNumberStr, null);
// throws NumberParseException
if (!phoneUtil.isValidNumber(phoneNumber)) {
throw new IllegalArgumentException(phoneNumberStr + " is not valid phone number");
}
}
示例4: validateThenFormatMobileNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
private String validateThenFormatMobileNumber(String phone) {
try {
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(phone, "+86");
if (phoneNumberUtil.isValidNumber(phoneNumber) && (
phoneNumberUtil.getNumberType(phoneNumber)
== PhoneNumberUtil.PhoneNumberType.MOBILE
|| phoneNumberUtil.getNumberType(phoneNumber)
== PhoneNumberUtil.PhoneNumberType.FIXED_LINE_OR_MOBILE)) {
return phoneNumberUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
} else {
return "";
}
} catch (NumberParseException e) {
return "";
}
}
示例5: isValid
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
public static boolean isValid(Context context, String aNumber) {
if (aNumber == null) { return false; }
PhoneNumberUtil pnu = PhoneNumberUtil.getInstance();
Locale locale = context.getResources().getConfiguration().locale;
String countryIso = getCurrentCountryIso(context, locale);
Phonenumber.PhoneNumber swissNumberProto;
try {
swissNumberProto = pnu.parse(aNumber, countryIso);
return pnu.isValidNumber(swissNumberProto)
// ignore ivory coast numbers (libphonenumber does not recognize those properly)
|| swissNumberProto.getCountryCode() == 225 ;
} catch (NumberParseException e) {
Log.w(LOGTAG, "isValid caught NumberParseException: number=" + aNumber);
return false;
}
}
示例6: getFormattedPhoneNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
private String getFormattedPhoneNumber() {
String phoneNumberText = getPhoneNumberText();
Country country = getCountry();
String formattedPhoneNumber = null;
Phonenumber.PhoneNumber phoneNumber;
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
try {
phoneNumber = phoneNumberUtil.parse(phoneNumberText, country.getIsoCode());
if (phoneNumberUtil.isValidNumber(phoneNumber)) {
formattedPhoneNumber = phoneNumberUtil.format(phoneNumber,
PhoneNumberUtil.PhoneNumberFormat.E164);
}
} catch (NumberParseException npe) {
formattedPhoneNumber = null;
}
return formattedPhoneNumber;
}
示例7: normalizeNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
public static String[] normalizeNumber(String phoneNumber, String defaultRegion) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber input;
try {
input = phoneUtil.parse(phoneNumber, defaultRegion.trim().toUpperCase(Locale.US));
if (phoneUtil.isValidNumber(input)) {
String parsedRegionCode = phoneUtil.getRegionCodeForNumber(input);
if (parsedRegionCode != null) {
return new String[] { phoneUtil.format(input, PhoneNumberUtil.PhoneNumberFormat.E164), parsedRegionCode };
}
else {
return null;
}
}
else {
return null;
}
}
catch (Exception e) {
return null;
}
}
示例8: phoneNumberToKontalkLocal
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
public static String phoneNumberToKontalkLocal(String number) {
PhoneNumberUtil pnUtil = PhoneNumberUtil.getInstance();
PhoneNumber n;
try {
n = pnUtil.parse(number, null);
} catch (NumberParseException ex) {
return "";
}
if (!pnUtil.isValidNumber(n))
return "";
return DigestUtils.sha1Hex(
PhoneNumberUtil.getInstance().format(n,
PhoneNumberUtil.PhoneNumberFormat.E164));
}
示例9: validatePhone
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
public static boolean validatePhone(String phone) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
Phonenumber.PhoneNumber number = phoneUtil.parse(phone, Locale.getDefault().getCountry());
return phoneUtil.isValidNumber(number);
} catch(NumberParseException e) {
e.printStackTrace();
return false;
}
}
示例10: formatPhoneNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
public static String formatPhoneNumber (String phone) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
Phonenumber.PhoneNumber number = phoneUtil.parse(phone, "US");
if (!phoneUtil.isValidNumber(number)) {
throw new Exception ("Phone number validation failed");
}
return phoneUtil.format(number, PhoneNumberUtil.PhoneNumberFormat.E164);
} catch (Exception e) {
// e.printStackTrace ();
System.out.print("Phone number validation failed for " + phone);
return "";
}
}
示例11: validate
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
private static Phonenumber.PhoneNumber validate(String number, String region) throws NumberParseException {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = phoneUtil.parse(number, region);
if (!phoneUtil.isValidNumber(phoneNumber)) {
throw new NumberParseException(NumberParseException.ErrorType.NOT_A_NUMBER, "Not a valid phone number:" + number);
}
return phoneNumber;
}
示例12: validPhoneNumberOrNull
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
private static Phonenumber.PhoneNumber validPhoneNumberOrNull(final Person person) {
try {
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
final Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(person.getPhoneNumber(), "FI");
return phoneNumberUtil.isValidNumber(phoneNumber) ? phoneNumber : null;
} catch (NumberParseException e) {
return null;
}
}
示例13: normalizeNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
/**
* Normalizes the passed in number, they should be only digits, some backends prepend + and maybe crazy users put in
* dashes or parentheses in the console
* @param number the number, e.g. "0783835665"
* @param countryCode the 2-letter country code, e.g. "RW"
* @return a pair of the normalized number and whether it looks like a possible full international number
*/
public static Pair<String, Boolean> normalizeNumber(String number, String countryCode) {
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
number = number.toLowerCase();
// if the number ends with e11, then that is Excel corrupting it, remove it
if (number.endsWith("e+11") || number.endsWith("e+12")) {
number = number.substring(0, number.length() - 4).replace(".", "");
}
// remove other characters
number = number.replaceAll("[^0-9a-z\\+]", "");
// add on a plus if it looks like it could be a fully qualified number
if (number.length() >= 11 && number.charAt(0) != '+') {
number = '+' + number;
}
try {
Phonenumber.PhoneNumber normalized = phoneUtil.parse(number, countryCode);
// now does it look plausible ?
if (phoneUtil.isValidNumber(normalized)) {
return new ImmutablePair<>(phoneUtil.format(normalized, PhoneNumberUtil.PhoneNumberFormat.E164), true);
}
} catch (NumberParseException ignored) {}
// this must be a local number of some kind, just lowercase and save
return new ImmutablePair<>(number.replaceAll("[^0-9a-z]", ""), false);
}
示例14: formatPhoneNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
private String formatPhoneNumber(final String phoneNumber) throws NumberParseException {
final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
final PhoneNumber number = phoneUtil.parse(phoneNumber, "DE");
if (!phoneUtil.isValidNumber(number)) {
throw new NumberParseException(NumberParseException.ErrorType.INVALID_COUNTRY_CODE,
"Number [" + phoneNumber + "] is invalid.");
}
return phoneUtil.format(number, PhoneNumberFormat.E164);
}
示例15: isValidPhoneNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
/**
* Checks whether a number is a valid phone number or not
*
* @param number The number to validate
*/
public static boolean isValidPhoneNumber(String number) {
final PhoneNumberUtil util = PhoneNumberUtil.getInstance();
try {
final Phonenumber.PhoneNumber phoneNumber = util.parse(number, getSimCountryCode());
//Using the | case is a quick hack to check validity of numbers which are returned wrongly as PhoneNumberUtil as invalid(fix for India)
return (util.isValidNumber(phoneNumber)) | (String.valueOf(phoneNumber.getNationalNumber()).length() == 10);
} catch (NumberParseException e) {
return false;
}
}