Get E64 contact format in Android API less than 16

I retrieve the list of phone numbers from the phone using the following code:

Cursor c = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null); while(c.moveToNext()){ Log.d(TAG,"NO.: "+ c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NORMALIZED_NUMBER))); } 

In API 16 and above, this will work fine for me, since I want ALL contacts formatted in E 164 format, regardless of how they are stored by the user.

  • However, for APIs below 16, this code does not work, and I cannot get the E 164 format for all contacts using the following line:

     c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 

The class "PhoneNumberUtils" cannot be used.

In addition, I cannot use the libphonenumber library to convert numbers to their E 164 format, since I do not know ISO-3166-1 the two-letter country code of the contacts.

Is there a way that I can get a two-digit country code according to ISO 3166-1 for each contact in Android so that I can use the libphonenumber library?

Or is there another solution to reach point 1.?

The numbers received from the phone can be of any format, for example.

  • Local: 965XXXXXXX
  • National: 0965XXXXXXXX
  • International: +91 96 5X XXXXXX
  • and others

whereas E 164 + 91XXXXXXXXXX, where 91 is the country code.

Any help would be greatly appreciated!

+6
source share
2 answers

You can use the network language for the default country code, accessible using getNetworkCountryIso . This method may not be reliable on CDMA networks (use getPhoneType to determine if there is a CDMA network).

This will be the same value that is used when calling a contact, so you should get the correct value.

For local numbers at the national level, I can not help you, because I do not have such functions in my country and do not know how it works in your company.

+3
source

Android did not save the number in E.16 format until Honeycomb. Therefore, you need to format it yourself using the libphonenumber library if you support lower API levels. The Contacts application uses the CountryDetector service to determine the country code, if one is not specified, and uses complex heuristics based on the information available on the device. This service is not part of the public API and in any case is not available for pre-cell, but you can copy the code to the ComprehensiveCountryDetector for reuse in your own application.

Please note that since the Honeycomb source is not available and I did not have the opportunity to test the Honeycomb device, I cannot comment on the state of Honeycomb associated with these problems.

0
source

Source: https://habr.com/ru/post/959293/


All Articles