本文整理匯總了Java中com.google.i18n.phonenumbers.PhoneNumberUtil.getRegionCodeForNumber方法的典型用法代碼示例。如果您正苦於以下問題:Java PhoneNumberUtil.getRegionCodeForNumber方法的具體用法?Java PhoneNumberUtil.getRegionCodeForNumber怎麽用?Java PhoneNumberUtil.getRegionCodeForNumber使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.i18n.phonenumbers.PhoneNumberUtil
的用法示例。
在下文中一共展示了PhoneNumberUtil.getRegionCodeForNumber方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的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;
}
}
示例2: syncCountryCodeSelector
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
/** Sync country code with text entered by the user, if possible. */
void syncCountryCodeSelector() {
try {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
CountryCode cc = (CountryCode) mCountryCode.getSelectedItem();
PhoneNumber phone = util.parse(mPhone.getText().toString(), cc != null ? cc.regionCode : null);
// autoselect correct country if user entered country code too
if (phone.hasCountryCode()) {
CountryCode ccLookup = new CountryCode();
ccLookup.regionCode = util.getRegionCodeForNumber(phone);
ccLookup.countryCode = phone.getCountryCode();
int position = ((CountryCodesAdapter) mCountryCode.getAdapter()).getPositionForId(ccLookup);
if (position >= 0) {
mCountryCode.setSelection(position);
}
}
}
catch (NumberParseException e) {
// ignored
}
}
示例3: 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;
}
}
示例4: parseNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
private static String parseNumber(String paramString) {
if (paramString == null) {
return null;
}
PhoneNumberUtil numberUtil = PhoneNumberUtil.getInstance();
String result;
try {
Phonenumber.PhoneNumber localPhoneNumber = numberUtil.parse(paramString, null);
result = numberUtil.getRegionCodeForNumber(localPhoneNumber);
if (result == null) {
return null;
}
} catch (NumberParseException localNumberParseException) {
return null;
}
return result;
}
示例5: formatNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
public static String formatNumber(String number, String localNumber)
throws InvalidNumberException
{
if (number == null) {
throw new InvalidNumberException("Null String passed as number.");
}
if (number.contains("@")) {
throw new InvalidNumberException("Possible attempt to use email address.");
}
number = number.replaceAll("[^0-9+]", "");
if (number.length() == 0) {
throw new InvalidNumberException("No valid characters found.");
}
// if (number.charAt(0) == '+')
// return number;
try {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
PhoneNumber localNumberObject = util.parse(localNumber, null);
String localCountryCode = util.getRegionCodeForNumber(localNumberObject);
Log.w(TAG, "Got local CC: " + localCountryCode);
PhoneNumber numberObject = util.parse(number, localCountryCode);
return util.format(numberObject, PhoneNumberFormat.E164);
} catch (NumberParseException e) {
Log.w(TAG, e);
return impreciseFormatNumber(number, localNumber);
}
}
示例6: formatNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
public static String formatNumber(String number, String localNumber)
throws InvalidNumberException
{
if (number.contains("@")) {
throw new InvalidNumberException("Possible attempt to use email address.");
}
number = number.replaceAll("[^0-9+]", "");
if (number.length() == 0) {
throw new InvalidNumberException("No valid characters found.");
}
if (number.charAt(0) == '+')
return number;
try {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
PhoneNumber localNumberObject = util.parse(localNumber, null);
String localCountryCode = util.getRegionCodeForNumber(localNumberObject);
Log.w(TAG, "Got local CC: " + localCountryCode);
PhoneNumber numberObject = util.parse(number, localCountryCode);
return util.format(numberObject, PhoneNumberFormat.E164);
} catch (NumberParseException e) {
Log.w(TAG, e);
return impreciseFormatNumber(number, localNumber);
}
}
示例7: formatNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
/**
* Format the phone number only if the given number hasn't been formatted.
* <p>
* The number which has only dailable character is treated as not being
* formatted.
*
* @param phoneNumber
* the number to be formatted.
* @param phoneNumberE164
* the E164 format number whose country code is used if the given
* phoneNumber doesn't have the country code.
* @param defaultCountryIso
* the ISO 3166-1 two letters country code whose convention will
* be used if the phoneNumberE164 is null or invalid, or if phoneNumber
* contains IDD.
* @return the formatted number if the given number has been formatted,
* otherwise, return the given number.
*
*/
@SuppressLint("Override")
public static String formatNumber(String phoneNumber, String phoneNumberE164, String defaultCountryIso) {
int len = phoneNumber.length();
for (int i = 0; i < len; i++) {
if (!isDialable(phoneNumber.charAt(i))) {
return phoneNumber;
}
}
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
// Get the country code from phoneNumberE164
if (phoneNumberE164 != null && phoneNumberE164.length() >= 2
&& phoneNumberE164.charAt(0) == '+') {
try {
// The number to be parsed is in E164 format, so the default region used doesn't
// matter.
Phonenumber.PhoneNumber pn = util.parse(phoneNumberE164, "ZZ");
String regionCode = util.getRegionCodeForNumber(pn);
if (!TextUtils.isEmpty(regionCode) &&
// This makes sure phoneNumber doesn't contain an IDD
normalizeNumber(phoneNumber).indexOf(phoneNumberE164.substring(1)) <= 0) {
defaultCountryIso = regionCode;
}
} catch (NumberParseException e) {
}
}
String result = formatNumber(phoneNumber, defaultCountryIso);
return result != null ? result : phoneNumber;
}
示例8: formatNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
public static String formatNumber(String number, String localNumber)
throws InvalidNumberException
{
if (number.contains("@")) {
throw new InvalidNumberException("Possible attempt to use email address.");
}
number = number.replaceAll("[^0-9+]", "");
if (number.length() == 0) {
throw new InvalidNumberException("No valid characters found.");
}
if (number.charAt(0) == '+')
return number;
try {
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
PhoneNumber localNumberObject = util.parse(localNumber, null);
String localCountryCode = util.getRegionCodeForNumber(localNumberObject);
Log.w("PhoneNumberFormatter", "Got local CC: " + localCountryCode);
PhoneNumber numberObject = util.parse(number, localCountryCode);
return util.format(numberObject, PhoneNumberFormat.E164);
} catch (NumberParseException e) {
Log.w("PhoneNumberFormatter", e);
return impreciseFormatNumber(number, localNumber);
}
}
示例9: formatNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
/**
* Format the phone number only if the given number hasn't been formatted.
* <p>
* The number which has only dailable character is treated as not being
* formatted.
*
* @param phoneNumber
* the number to be formatted.
* @param phoneNumberE164
* the E164 format number whose country code is used if the given
* phoneNumber doesn't have the country code.
* @param defaultCountryIso
* the ISO 3166-1 two letters country code whose convention will
* be used if the phoneNumberE164 is null or invalid, or if phoneNumber
* contains IDD.
* @return the formatted number if the given number has been formatted,
* otherwise, return the given number.
*
* @hide
*/
public static String formatNumber(
String phoneNumber, String phoneNumberE164, String defaultCountryIso) {
int len = phoneNumber.length();
for (int i = 0; i < len; i++) {
if (!isDialable(phoneNumber.charAt(i))) {
return phoneNumber;
}
}
PhoneNumberUtil util = PhoneNumberUtil.getInstance();
// Get the country code from phoneNumberE164
if (phoneNumberE164 != null && phoneNumberE164.length() >= 2
&& phoneNumberE164.charAt(0) == '+') {
try {
// The number to be parsed is in E164 format, so the default region used doesn't
// matter.
PhoneNumber pn = util.parse(phoneNumberE164, "ZZ");
String regionCode = util.getRegionCodeForNumber(pn);
if (!TextUtils.isEmpty(regionCode) &&
// This makes sure phoneNumber doesn't contain an IDD
normalizeNumber(phoneNumber).indexOf(phoneNumberE164.substring(1)) <= 0) {
defaultCountryIso = regionCode;
}
} catch (NumberParseException e) {
}
}
String result = formatNumber(phoneNumber, defaultCountryIso);
return result != null ? result : phoneNumber;
}
示例10: formatNumber
import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
public static String formatNumber(Context context, String number)
{
if (_cc == null) {
TelephonyManager tm = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
_cc = tm.getSimCountryIso();
if (_cc == null) {
_cc = "us";
}
}
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber phoneNumber = null;
try {
phoneNumber = phoneUtil.parse(number, _cc);
} catch (Exception ex) {
return number;
}
String fmt, cc = phoneUtil.getRegionCodeForNumber(phoneNumber);
if (cc == null || !cc.equalsIgnoreCase(_cc)) {
fmt = phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
} else {
fmt = phoneUtil.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
}
return fmt != null ? fmt : number;
}