Formatting phone number to E164 format in Android
Asked Answered
I

2

8

I want convert every phone number from conatct in device to E164 format. So, I used opensource below.

libphonenumber

So I used it like here.

Phonenumber.PhoneNumber formattedNumber = null;
String formatted = null;

try {
    formattedNumber = phoneUtil.parse(phoneNumber, "KR");
    formatted = phoneUtil.format(formattedNumber,PhoneNumberUtil.PhoneNumberFormat.E164);

    if (StringUtils.isEmpty(formatted) == false && formatted.length() > 0 && StringUtils.isEmpty(name) == false && name.length() > 0) {
        listName.add(name);
        listPhoneNumber.add(formatted);
    }
} catch (NumberParseException e) {
    continue;
}

And I read that this library is used by the Android framework since 4.0.

The Java version is optimized for running on smartphones, and is used by the Android framework since 4.0 (Ice Cream Sandwich).

I want to use this from Android SDK. So I found this. Android SDK provides this PhoneNumberUtils .

And there is a function

formatNumberToE164(String phoneNumber, String defaultCountryIso)

It's really easy to use. but API level of this function is 21.

So My question is..How can I use PhoneNumberUtils to convert phonenumber to E164 under API Level 14(ICS) ~ 21?

Thanks.!

Insubstantial answered 14/5, 2015 at 6:44 Comment(0)
M
9

The problem is PhoneNumberUtils.formatNumberToE164(...) is not available on older devices and there's nothing else in PhoneNumberUtils that does the same job.

I'd suggest using PhoneNumberUtils when available and libphonenumber on older devices, for example:

public String formatE164Number(String countryCode, String phNum) {

    String e164Number;
    if (TextUtils.isEmpty(countryCode)) {
        e164Number = phNum;
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            e164Number = PhoneNumberUtils.formatNumberToE164(phNum, countryCode);
        } else {
            try {
                PhoneNumberUtil instance = PhoneNumberUtil.getInstance();
                Phonenumber.PhoneNumber phoneNumber = instance.parse(phNum, countryCode);
                e164Number = instance.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);

            } catch (NumberParseException e) {
                Log.e(TAG, "Caught: " + e.getMessage(), e);
                e164Number = phNum;
            }
        }
    }

    return e164Number;
}
Magnetostriction answered 6/6, 2016 at 20:58 Comment(0)
S
0

I had tried above code its not working for me also, need to made small changes, am sharing updated code :

public String formatE164Number(String countryCode, String phNum) {

    String e164Number;
    if (TextUtils.isEmpty(countryCode)) {
        e164Number = phNum;
    } else {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            /*e164Number = PhoneNumberUtils.formatNumberToE164(phNum, countryCode);*/
            e164Number =PhoneNumberUtils.formatNumber(phNum, countryCode);
            Timber.d("e164Number E164 :"+ e164Number);
            Toast.makeText(this,"e164 : "+e164Number, Toast.LENGTH_SHORT).show();
        } else {
            try {
                PhoneNumberUtil instance = PhoneNumberUtil.getInstance();
                Phonenumber.PhoneNumber phoneNumber = instance.parse(phNum, countryCode);
                e164Number = instance.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.E164);
                Timber.d("e164Number E164 : $e164Number");
                Timber.d("e164Number INTERNATIONAL  : "+instance.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.INTERNATIONAL));
                Timber.d("e164Number NATIONAL :  "+instance.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.NATIONAL));
                Timber.d("e164Number RFC3966 :  "+instance.format(phoneNumber, PhoneNumberUtil.PhoneNumberFormat.RFC3966));

            } catch (NumberParseException e) {
                Log.e(TAG, "Caught: " + e.getMessage(), e);
                e164Number = phNum;
            }
        }
    }

    return e164Number;
}

Now you can method like this :

 formatE164Number("US","5712712384");
 (571) 271-2384 


formatE164Number("US","+15712712384");
+1 571-271-2384
Storekeeper answered 7/9, 2021 at 4:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.