Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get E 164 format of contacts in Android API less than 16

I retrieve the list of contact 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 would work perfectly for me, as I want ALL the contacts formatted in the E 164 format, no matter how they are stored by the user.

  1. However, for APIs below 16, the above code won't work and I am not able to get the E 164 format for all contacts by using the following line:

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

The 'PhoneNumberUtils' class could not come to any use.

Also, I cannot use the libphonenumber library to convert numbers to their E 164 format, as I don't know the ISO 3166-1 two letter country-code of the contacts.

Is there any way in which I can obtain the ISO 3166-1 two letter country-code for each contact in Android, so that I can use the libphonenumber library ?

Or is there any other solution to achieve point 1. ?

The numbers retrieved from the phone can be of any format eg.

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

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

Any help would be much appreciated !

like image 865
geekoraul Avatar asked Dec 04 '13 00:12

geekoraul


1 Answers

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

It will be the same value used when phoning the contact, so you should get the correct value.

For local to national numbers, I can't help you as I don't have such features in my country and don't know how it works in yours.

like image 152
Geoffroy Avatar answered Oct 27 '22 01:10

Geoffroy