本文整理汇总了Java中com.google.i18n.phonenumbers.PhoneNumberUtil.parse方法的典型用法代码示例。如果您正苦于以下问题:Java PhoneNumberUtil.parse方法的具体用法?Java PhoneNumberUtil.parse怎么用?Java PhoneNumberUtil.parse使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.i18n.phonenumbers.PhoneNumberUtil
的用法示例。
在下文中一共展示了PhoneNumberUtil.parse方法的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: 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;
}
}
示例3: 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);
}
}
示例4: 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]", "");
}
示例5: 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);
}
示例6: 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;
}
示例7: 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;
}
示例8: isValidNumberInE164Format
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的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));
}
示例9: 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(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);
}
}
示例10: 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;
}
}
示例11: 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");
}
}
示例12: 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 "";
}
}
示例13: testPhoneNumberParse3
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
@Test
public void testPhoneNumberParse3() throws Exception {
String phoneNumberString = "072 916 6903";
PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
try {
Phonenumber.PhoneNumber phoneNumber = phoneNumberUtil.parse(phoneNumberString,"ZA");
log.info("{}", phoneNumberUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164));
log.info("Country Code: {}", phoneNumber.getCountryCode()) ;
log.info("Country Source: {}", phoneNumber.getCountryCodeSource()) ;
log.info("National Number: {}", phoneNumber.getNationalNumber()) ;
log.info("National Number: {}", phoneNumber.getNationalNumber()) ;
assertTrue( phoneNumberUtil.isValidNumber(phoneNumber));
}catch (Exception e)
{
log.error(e.getMessage());
}
}
示例14: 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;
}
}
示例15: isTaggableDestination
import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
public static boolean isTaggableDestination(Recipients recipients){
// Be safe - err on the side of not tagging
if (recipients.isGroupRecipient())
return false;
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
try {
PhoneNumber num = util.parse(recipients.getPrimaryRecipient().getNumber(),
Locale.getDefault().getCountry());
PhoneNumberType type = util.getNumberType(num);
Log.d(TAG, "Number type: " + type.toString());
return type == PhoneNumberType.FIXED_LINE ||
type == PhoneNumberType.MOBILE ||
type == PhoneNumberType.FIXED_LINE_OR_MOBILE;
}
catch (NumberParseException e){
Log.w(TAG, "Couldn't get number type (country: " + Locale.getDefault().getCountry() + ")");
return false;
}
}