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


Java PhoneNumberUtil.format方法代碼示例

本文整理匯總了Java中com.google.i18n.phonenumbers.PhoneNumberUtil.format方法的典型用法代碼示例。如果您正苦於以下問題:Java PhoneNumberUtil.format方法的具體用法?Java PhoneNumberUtil.format怎麽用?Java PhoneNumberUtil.format使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.google.i18n.phonenumbers.PhoneNumberUtil的用法示例。


在下文中一共展示了PhoneNumberUtil.format方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: 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

示例2: 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

示例3: 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

示例4: 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;
    }
}
 
開發者ID:whereuat,項目名稱:whereuat-android,代碼行數:25,代碼來源:PhonebookUtils.java

示例5: 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

示例6: 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

示例7: normalize

import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
/**
     * normalize number to E164 format. Returns null if null is provided.
     *
     * @param aNumber the phone number to normalize
     * @return e164 phone number string
     */
    public static String normalize(Context context, String aNumber) {
        if (aNumber == null) { return null; }

        PhoneNumberUtil pnu = PhoneNumberUtil.getInstance();
        Locale locale = context.getResources().getConfiguration().locale;
        String countryIso = getCurrentCountryIso(context, locale);
        try {
//            Log.d(LOGTAG, countryIso);
            String numberE164 = pnu.format(pnu.parse(aNumber, countryIso), PhoneNumberUtil.PhoneNumberFormat.E164);
//            Log.d(LOGTAG, numberE164);
            return numberE164;
        } catch (NumberParseException e) {
            Log.w(LOGTAG, "normalize caught NumberParseException: number=" + aNumber);
            return aNumber;
        }
    }
 
開發者ID:doerfli,項目名稱:leavemealone,代碼行數:23,代碼來源:PhoneNumberHelper.java

示例8: getFormattedPhoneNumber

import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
private String getFormattedPhoneNumber() {
    String phoneNumberText = getPhoneNumberText();
    Country country = getCountry();

    String formattedPhoneNumber = null;
    Phonenumber.PhoneNumber phoneNumber;
    PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    try {
        phoneNumber = phoneNumberUtil.parse(phoneNumberText, country.getIsoCode());
        if (phoneNumberUtil.isValidNumber(phoneNumber)) {
            formattedPhoneNumber = phoneNumberUtil.format(phoneNumber,
                    PhoneNumberUtil.PhoneNumberFormat.E164);
        }
    } catch (NumberParseException npe) {
        formattedPhoneNumber = null;
    }

    return formattedPhoneNumber;
}
 
開發者ID:arpankapoor,項目名稱:VerifyMobileNumber,代碼行數:20,代碼來源:MainActivity.java

示例9: 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:Securecom,項目名稱:Securecom-Messaging,代碼行數:17,代碼來源:PhoneNumberFormatter.java

示例10: 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 npe) {
    Log.w("CreateAccountActivity", npe);
  } catch (NumberFormatException nfe) {
    Log.w("CreateAccountActivity", nfe);
  }

  return "+"                                                     +
      countryCode.replaceAll("[^0-9]", "").replaceAll("^0*", "") +
      number.replaceAll("[^0-9]", "");
}
 
開發者ID:Securecom,項目名稱:Securecom-Text,代碼行數:19,代碼來源:PhoneNumberFormatter.java

示例11: 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

示例12: convertToModel

import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
/** {@inheritDoc} */
@Override
public String convertToModel(final String value, final Class<? extends String> targetType, final Locale locale) throws ConversionException {
    if (isNullOrEmpty(value))
        return null;

    final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    final String country = locale == null ? "RU" : locale.getCountry();
    Phonenumber.PhoneNumber phone = null;
    try {
        phone = phoneUtil.parse(value, country);
    } catch (final NumberParseException e) {
        throw new ConversionException("Неправильный формат телефона", e);
    }

    return phoneUtil.format(phone, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
}
 
開發者ID:ExtaSoft,項目名稱:extacrm,代碼行數:18,代碼來源:PhoneConverter.java

示例13: convertToPresentation

import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
/** {@inheritDoc} */
@Override
public String convertToPresentation(final String value, final Class<? extends String> targetType, final Locale locale) throws ConversionException {
    if (isNullOrEmpty(value))
        return null;

    final PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    final String country = locale == null ? "RU" : locale.getCountry();
    Phonenumber.PhoneNumber phone = null;
    try {
        phone = phoneUtil.parse(value, country);
    } catch (final NumberParseException e) {
        throw new ConversionException("Неправильный формат телефона", e);
    }

    return phoneUtil.format(phone, PhoneNumberUtil.PhoneNumberFormat.NATIONAL);
}
 
開發者ID:ExtaSoft,項目名稱:extacrm,代碼行數:18,代碼來源:PhoneConverter.java

示例14: getInternationalNumber

import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
public String getInternationalNumber(String phoneNo) throws NumberParseException {
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    if (HTTextUtils.isEmpty(phoneNo))
        return null;

    Phonenumber.PhoneNumber number = phoneUtil.parse(phoneNo, getCountryCode());
    return phoneUtil.format(number, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL);
}
 
開發者ID:hypertrack,項目名稱:hypertrack-live-android,代碼行數:9,代碼來源:HyperTrackLiveUser.java

示例15: formatNumberInternational

import com.google.i18n.phonenumbers.PhoneNumberUtil; //導入方法依賴的package包/類
public static String formatNumberInternational(String number) {
  try {
    PhoneNumberUtil util     = PhoneNumberUtil.getInstance();
    PhoneNumber parsedNumber = util.parse(number, null);
    return util.format(parsedNumber, PhoneNumberFormat.INTERNATIONAL);
  } catch (NumberParseException e) {
    Log.w(TAG, e);
    return number;
  }
}
 
開發者ID:XecureIT,項目名稱:PeSanKita-lib,代碼行數:11,代碼來源:PhoneNumberFormatter.java


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