15

The library libphonenumber doesn't work on Android and the exception says: "Caused by: java.lang.UnsupportedOperationException: CANON_EQ flag not supported"

The description in the webpage says that the lib is for "Smartphones" but Android doesn't support CANON_EQ reg_ex flag..

Am I missing something?

Joachim Sauer
  • 302,674
  • 57
  • 556
  • 614
shaimagz
  • 1,265
  • 4
  • 17
  • 39
  • Most of the features of this library are available with the android framework itself, Please read https://developer.android.com/reference/android/telephony/PhoneNumberUtils.html – humble_wolf Aug 10 '19 at 18:13

8 Answers8

30

If you're using Android studio use

implementation 'com.googlecode.libphonenumber:libphonenumber:8.12.32'

If you want to download the latest JAR file goto

http://mvnrepository.com/artifact/com.googlecode.libphonenumber/libphonenumber

AliSh
  • 10,085
  • 5
  • 44
  • 76
Bala Vishnu
  • 2,588
  • 23
  • 20
9

Hi Please use this function to pass phone number and country code like india 91

public static String parseContact(String contact, String countrycode) {
    PhoneNumber phoneNumber = null;
    PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
    String finalNumber = null;
    String isoCode = phoneNumberUtil.getRegionCodeForCountryCode(Integer.parseInt(countrycode));
    boolean isValid = false;
    PhoneNumberType isMobile = null;
    try {
        phoneNumber = phoneNumberUtil.parse(contact, isoCode);
        isValid = phoneNumberUtil.isValidNumber(phoneNumber);
        isMobile = phoneNumberUtil.getNumberType(phoneNumber);

    } catch (NumberParseException e) {
        e.printStackTrace();
    } catch (NullPointerException e) {
        e.printStackTrace();
    }


    if (isValid
            && (PhoneNumberType.MOBILE == isMobile || PhoneNumberType.FIXED_LINE_OR_MOBILE == isMobile)) {
        finalNumber = phoneNumberUtil.format(phoneNumber,
                PhoneNumberFormat.E164).substring(1);
    }

    return finalNumber;
}
Vijay Rajput
  • 1,091
  • 1
  • 13
  • 18
6

Please try using libphonenumber-2.5.1.jar from the download page:

http://code.google.com/p/libphonenumber/downloads/list

The jar from the Right-Number project works because they are using libphonenumber-2.4.jar. The CANON_EQ flag was introduced in v2.5, but is now removed from 2.5.1 to be compatible with Android until it supports the flag.

2

compile 'com.googlecode.libphonenumber:libphonenumber:7.1.1'

Use this dependency in you gradle.

check this https://github.com/googlei18n/libphonenumber

Petter Friberg
  • 21,252
  • 9
  • 60
  • 109
  • Worked! I tried downloading the latest jar from their repo (libphonenumber-7.7.2) and it kept throwing exception in runtime. With this older version and with import via gradle it works!!!! – user2924714 Nov 01 '16 at 11:57
2

On android :

implementation 'com.googlecode.libphonenumber:libphonenumber:8.12.31'
MakiX
  • 326
  • 3
  • 8
1

I've struggled with the exact same question myself, and was able to solve it by using the following version:

http://code.google.com/p/right-number/source/browse/trunk/RightNumber/libs/libphonenumber.jar

This version is from the Right-Number project that aims to build a user interface for dealing with phone number formatting on Android.

I've used it in my project and it works fine.

Roy Sharon
  • 3,488
  • 4
  • 24
  • 34
1

Submit an issue at:

http://code.google.com/p/libphonenumber/issues/list

I had a similar issue with the newest revision and they were very fast with answering my issue. It's definitely just a bug which has surfaced and you should report it if you want it fixed. Took them 10 minutes to acknowledge my issue and 15 min after that they said they had a patch ready for it.

The jar at Right-Number is just an older revision.

Henrik P
  • 13
  • 2
1

The new version of libphonenumber http://code.google.com/p/libphonenumber/ is compiled for Java 1.5 and will now work on older android systems.

Previous versions were compiled for Java 1.6, but you could have re-compiled for any version using the supplied source code.

g1smd
  • 11
  • 1