当前位置: 首页>>代码示例>>Java>>正文


Java PhoneNumberUtil.getInstance方法代码示例

本文整理汇总了Java中com.google.i18n.phonenumbers.PhoneNumberUtil.getInstance方法的典型用法代码示例。如果您正苦于以下问题:Java PhoneNumberUtil.getInstance方法的具体用法?Java PhoneNumberUtil.getInstance怎么用?Java PhoneNumberUtil.getInstance使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.google.i18n.phonenumbers.PhoneNumberUtil的用法示例。


在下文中一共展示了PhoneNumberUtil.getInstance方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: 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;
  }
}
 
开发者ID:CableIM,项目名称:Cable-Android,代码行数:25,代码来源:ShortCodeUtil.java

示例2: 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);
  }
}
 
开发者ID:XecureIT,项目名称:PeSanKita-android,代码行数:24,代码来源:RegistrationActivity.java

示例3: 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);
		}
	}
}
 
开发者ID:mityung,项目名称:XERUNG,代码行数:18,代码来源:SignIn.java

示例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]", "");
}
 
开发者ID:XecureIT,项目名称:PeSanKita-lib,代码行数:17,代码来源:PhoneNumberFormatter.java

示例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);
}
 
开发者ID:AppHero2,项目名称:Raffler-Android,代码行数:23,代码来源:ChatListRecyclerViewAdapter.java

示例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;
}
 
开发者ID:project-sunbird,项目名称:sunbird-utils,代码行数:24,代码来源:ProjectUtil.java

示例7: 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");
    }
}
 
开发者ID:Cryptonomica,项目名称:cryptonomica,代码行数:18,代码来源:TwilioUtils.java

示例8: 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;
}
 
开发者ID:arunrajora,项目名称:Chit-Chat,代码行数:20,代码来源:Utils.java

示例9: 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 "";
    }
}
 
开发者ID:Piasy,项目名称:YaMvp,代码行数:18,代码来源:MainPresenter.java

示例10: 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));
}
 
开发者ID:Aptoide,项目名称:aptoide-client-v8,代码行数:18,代码来源:ContactUtils.java

示例11: 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);
  }
}
 
开发者ID:redcracker,项目名称:TextSecure,代码行数:24,代码来源:RegistrationActivity.java

示例12: getPhoneNumber

import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
public static Phonenumber.PhoneNumber getPhoneNumber(Context context, String number,
        Locale locale) {
    if (TextUtils.isEmpty(number)) {
        return null;
    }

    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    String countryIso = getCurrentCountryIso(context, locale);
    Phonenumber.PhoneNumber pn = null;
    try {
        pn = util.parse(number, countryIso);
    } catch (NumberParseException e) {
        Log.v(TAG, "getGeoDescription: NumberParseException for incoming number '" +
                number + "'");
    }
    return pn;
}
 
开发者ID:xdtianyu,项目名称:PhoneNumber,代码行数:18,代码来源:GoogleNumberHandler.java

示例13: getDisplay

import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
/**
 * Gets a representation of the URN for display
 */
public String getDisplay(Org org, boolean full) {
    if (org.isAnon()) {
        return ANON_MASK;
    }

    if (m_scheme == Scheme.TEL && !full) {
        // if we don't want a full tell, see if we can show the national format instead
        try {
            if (StringUtils.isNotEmpty(m_path) && m_path.charAt(0) == '+') {
                PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
                return phoneUtil.format(phoneUtil.parse(m_path, null), PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
            }
        }
        catch (Exception ignored) {}
    }

    return m_path;
}
 
开发者ID:rapidpro,项目名称:flows,代码行数:22,代码来源:ContactUrn.java

示例14: formatNumber

import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
/**
 * Format a phone number.
 * <p>
 * If the given number doesn't have the country code, the phone will be
 * formatted to the default country's convention.
 *
 * @param phoneNumber
 *            the number to be formatted.
 * @param defaultCountryIso
 *            the ISO 3166-1 two letters country code whose convention will
 *            be used if the given number doesn't have the country code.
 * @return the formatted number, or null if the given number is not valid.
 *
 */
@SuppressLint("Override")
public static String formatNumber(String phoneNumber, String defaultCountryIso) {
    // Do not attempt to format numbers that start with a hash or star symbol.
    if (phoneNumber.startsWith("#") || phoneNumber.startsWith("*")) {
        return phoneNumber;
    }

    PhoneNumberUtil util = PhoneNumberUtil.getInstance();
    String result = null;
    try {
        Phonenumber.PhoneNumber pn = util.parseAndKeepRawInput(phoneNumber, defaultCountryIso);
        result = util.formatInOriginalFormat(pn, defaultCountryIso);
    } catch (NumberParseException e) {
    }
    return result;
}
 
开发者ID:moezbhatti,项目名称:qksms,代码行数:31,代码来源:PhoneNumberUtils.java

示例15: testGetGeocodedLocationFor

import com.google.i18n.phonenumbers.PhoneNumberUtil; //导入方法依赖的package包/类
@Test
public void testGetGeocodedLocationFor() throws Exception {
	final List<String> numberList = Arrays.asList("10086",
			"15573275419", "057157892157");
	try {
		Locale locale = Locale.CHINA;
		final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();

		for (String s : numberList) {
			Phonenumber.PhoneNumber phoneNumber = GeocoderUtils.parsePhoneNumber(s, locale.getCountry());
			System.out.println("geocoder: " + GeocoderUtils.getDescriptionForNumber(phoneNumber, locale));
		}
	} catch (NumberParseException e) {
		System.err.println("NumberParseException was thrown: " + e.toString());
	}
}
 
开发者ID:waynell,项目名称:DialerBox,代码行数:17,代码来源:GeocoderUtilsTest.java


注:本文中的com.google.i18n.phonenumbers.PhoneNumberUtil.getInstance方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。