當前位置: 首頁>>代碼示例>>Java>>正文


Java PhoneNumberUtil.getRegionCodeForNumber方法代碼示例

本文整理匯總了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;
  }
}
 
開發者ID:CableIM,項目名稱:Cable-Android,代碼行數:25,代碼來源:ShortCodeUtil.java

示例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
    }
}
 
開發者ID:kontalk,項目名稱:androidclient,代碼行數:22,代碼來源:NumberValidation.java

示例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;
	}
}
 
開發者ID:delight-im,項目名稱:Faceless,代碼行數:23,代碼來源:Global.java

示例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;
}
 
開發者ID:dlukashev,項目名稱:android-phone-number-with-flags,代碼行數:18,代碼來源:PhoneUtils.java

示例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);
    }
  }
 
開發者ID:XecureIT,項目名稱:PeSanKita-lib,代碼行數:35,代碼來源:PhoneNumberFormatter.java

示例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);
  }
}
 
開發者ID:redcracker,項目名稱:TextSecure,代碼行數:31,代碼來源:PhoneNumberFormatter.java

示例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;
}
 
開發者ID:moezbhatti,項目名稱:qksms,代碼行數:48,代碼來源:PhoneNumberUtils.java

示例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);
  }
}
 
開發者ID:Securecom,項目名稱:Securecom-Text,代碼行數:31,代碼來源:PhoneNumberFormatter.java

示例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;
}
 
開發者ID:SilentCircle,項目名稱:silent-contacts-android,代碼行數:49,代碼來源:PhoneNumberHelper.java

示例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;
}
 
開發者ID:RoliSoft,項目名稱:RS-Text-Forwarder-Android-Client,代碼行數:31,代碼來源:ContactTools.java


注:本文中的com.google.i18n.phonenumbers.PhoneNumberUtil.getRegionCodeForNumber方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。