本文整理汇总了Java中com.google.i18n.phonenumbers.NumberParseException类的典型用法代码示例。如果您正苦于以下问题:Java NumberParseException类的具体用法?Java NumberParseException怎么用?Java NumberParseException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
NumberParseException类属于com.google.i18n.phonenumbers包,在下文中一共展示了NumberParseException类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: sendOTPNumber
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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: isShortCode
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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;
}
}
示例3: toInternationalFormat
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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;
}
示例4: getUserParams
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的package包/类
private UserParams getUserParams(final Context context, final String name, final String number, String ISOCode, File profileImage)
throws NumberParseException {
final HyperTrackLiveUser user = this.onboardingManager.getUser();
// Update Country Code from device's current location
if (!HTTextUtils.isEmpty(ISOCode))
user.setCountryCode(ISOCode);
String encodedImage = null;
// Set user's profile image
if (profileImage != null && profileImage.length() > 0) {
user.setPhotoImage(profileImage);
byte[] bytes = convertFiletoByteArray(profileImage);
if (bytes != null && bytes.length > 0)
encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT);
}
HyperTrackLiveUser.setHyperTrackLiveUser(context);
String phoneNumber = user.getInternationalNumber(number);
return new UserParams()
.setName(name)
.setPhone(phoneNumber)
.setPhoto(encodedImage)
.setLookupId(phoneNumber);
}
示例5: initializeNumber
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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: formatE164
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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]", "");
}
示例7: updateContactName
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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);
}
示例8: parseNumber
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的package包/类
public static String parseNumber(String number) {
if (!Pattern.matches(PHONE_NUMBER_REGEX, number)) {
LOGGER.error("invalid phone number. {}", number);
return null;
}
if (Pattern.matches(SHORT_PHONE_NUMBER_REGEX, number)) {
number = "+86" + number;
}
try {
PhoneNumber phoneNumber = PN_Util.parse(number, "CH");
if (PN_Util.isValidNumber(phoneNumber)) {
return String.valueOf(phoneNumber.getNationalNumber());
}
LOGGER.error("invalid phone number. {}", number);
return null;
} catch (NumberParseException e) {
LOGGER.error("invalid phone number. {}", number);
return null;
}
}
示例9: toMsisdn
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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.NumberParseException; //导入依赖的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: isValidMobileNumber
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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;
}
示例12: isValidNumberInE164Format
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的package包/类
public boolean isValidNumberInE164Format(String number) {
final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
Phonenumber.PhoneNumber phoneNumber = null;
int country;
try {
phoneNumber = phoneNumberUtil.parse(number, "");
country = phoneNumber.getCountryCode();
} catch (NumberParseException e) {
e.printStackTrace();
return false;
}
return phoneNumberUtil.isValidNumberForRegion(phoneNumber,
phoneNumberUtil.getRegionCodeForCountryCode(country));
}
示例13: initializeNumber
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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(localNumberObject.getCountryCode()+"");
this.number.setText(localNumberObject.getNationalNumber()+"");
}
} else {
String simCountryIso = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getSimCountryIso();
if (!TextUtils.isEmpty(simCountryIso)) {
this.countryCode.setText(numberUtil.getCountryCodeForRegion(simCountryIso.toUpperCase())+"");
}
}
} catch (NumberParseException npe) {
Log.w("CreateAccountActivity", npe);
}
}
示例14: convertToE164
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的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;
}
}
示例15: sendTestSms
import com.google.i18n.phonenumbers.NumberParseException; //导入依赖的package包/类
@ApiMethod(
name = "sendTestSms",
path = "sendTestSms",
httpMethod = ApiMethod.HttpMethod.POST
)
@SuppressWarnings("unused")
public StringWrapperObject sendTestSms(
// final HttpServletRequest httpServletRequest,
final User googleUser,
final @Named("phoneNumber") String phoneNumber,
final @Named("smsMessage") String smsMessage
// see: https://cloud.google.com/appengine/docs/java/endpoints/exceptions
) throws UnauthorizedException, BadRequestException, NotFoundException, NumberParseException,
IllegalArgumentException, TwilioRestException {
/* --- Check authorization: */
CryptonomicaUser cryptonomicaUser = UserTools.ensureCryptonomicaOfficer(googleUser);
/* --- Send SMS */
Message message = TwilioUtils.sendSms(phoneNumber, smsMessage);
return new StringWrapperObject(message.toJSON());
}