android 将电话号码转换为国际格式

发布于 2024-08-21 06:25:44 字数 204 浏览 4 评论 0原文

我想知道是否可以转换电话号码 拨出电话时转换为国际格式。

例如,如果一个法国用户(抱歉,这是我知道的唯一格式) 不会犯错误:-) 尝试使用国家格式调用: 01.47.12.34.56 然后一个方法会将其转换为国际格式 像这样:+33.1.47.12.34.56

我已经查看了 PhoneNumberUtils 的文档,但我不知道是否 有一种方法可以做我想做的事。

I'd like to know whether it's possible to have phone numbers converted
into international format when a call is outgoing.

For instance, if a french user (sorry it's the only format i know i
won't make a mistake :-) try to call with the national format :
01.47.12.34.56 then a method will convert it into international format
like this : +33.1.47.12.34.56

I've looked into the doc of the PhoneNumberUtils but i don't know if
there is a method doing what i want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

一抹苦笑 2024-08-28 06:25:44

老帖子,但如果它对任何人有帮助:使用谷歌的 libphonenumber 库进行各种格式化和验证电话号码。在这种特殊情况下,要转换为国际格式,请使用 PhoneNumberUtil 中同一个图书馆。

Old post, but if it helps anyone: use google's libphonenumber library to do all sorts of formatting and validating for phone numbers. In this particular case, to convert to international format use the format(PhoneNumber number, PhoneNumberFormat format) api in class PhoneNumberUtil in the same library.

踏雪无痕 2024-08-28 06:25:44

该数字不是以字符串形式出现吗?

如果我没记错的话,使用Java的字符串方法你可以自己轻松地做到这一点?

所以获取字符串,去掉 0 并添加 +33

Doesn't the number come as a String?

Using Java's string methods you could do this yourself easily if I'm not mistaken?

So get the String, cop the 0 off and add the +33

一袭水袖舞倾城 2024-08-28 06:25:44

使用此方法将您的本地号码转换为国际格式,我正在使用 google 库 https://github.com/google /libphonenumber 正如 (Aswin Kumar) 提到的

Java

   public static String formatPhoneNumber(String phoneNumber) {
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    Phonenumber.PhoneNumber formattedNumber = null;
    String formatted = null;

    try {
        TelephonyManager manager = (TelephonyManager) sContext.getSystemService(Context.TELEPHONY_SERVICE);
        String countryCode = manager.getNetworkCountryIso();
        formattedNumber = phoneUtil.parse(phoneNumber, countryCode.toUpperCase());
        formatted = phoneUtil.format(formattedNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
        return formatted;
    } catch (NumberParseException e) {
        e.printStackTrace();
    }
    return null;
}

Kotlin

    private fun formatNumber(number: String?) {
    val phoneUtil = PhoneNumberUtil.getInstance()
    var formattedNumber: PhoneNumber? = null
    var formatted: String? = null
    try {
        val manager = applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val networkCountryIso: String = manager.networkCountryIso
        Log.i("SERVICE " , "ISO "+ networkCountryIso)
        formattedNumber = phoneUtil.parse(number, networkCountryIso.toUpperCase())
        formatted = phoneUtil.format(formattedNumber, PhoneNumberUtil.PhoneNumberFormat.E164)
        Log.i("SERVICE " , "phone number"+ formatted)
        findCallerId(formatted)
    } catch (e: NumberParseException) {
        e.printStackTrace()
    }
}

Use this method to convert your local number to International Format, I am using google library https://github.com/google/libphonenumber as (Aswin Kumar) mentioned

Java

   public static String formatPhoneNumber(String phoneNumber) {
    PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
    Phonenumber.PhoneNumber formattedNumber = null;
    String formatted = null;

    try {
        TelephonyManager manager = (TelephonyManager) sContext.getSystemService(Context.TELEPHONY_SERVICE);
        String countryCode = manager.getNetworkCountryIso();
        formattedNumber = phoneUtil.parse(phoneNumber, countryCode.toUpperCase());
        formatted = phoneUtil.format(formattedNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
        return formatted;
    } catch (NumberParseException e) {
        e.printStackTrace();
    }
    return null;
}

Kotlin

    private fun formatNumber(number: String?) {
    val phoneUtil = PhoneNumberUtil.getInstance()
    var formattedNumber: PhoneNumber? = null
    var formatted: String? = null
    try {
        val manager = applicationContext.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val networkCountryIso: String = manager.networkCountryIso
        Log.i("SERVICE " , "ISO "+ networkCountryIso)
        formattedNumber = phoneUtil.parse(number, networkCountryIso.toUpperCase())
        formatted = phoneUtil.format(formattedNumber, PhoneNumberUtil.PhoneNumberFormat.E164)
        Log.i("SERVICE " , "phone number"+ formatted)
        findCallerId(formatted)
    } catch (e: NumberParseException) {
        e.printStackTrace()
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文