Android - Contact from number with Country Code
I had a same problem last month and I managed to solve it using the google's libphonenumber library.
The problem only occurs with local/national phone numbers as they can be saved without country code or using '0'.
For example :
A typical phone number in India can be saved in following formats
a. +919665123456
b. 919665123456
c. 09665123456
d. 9665123456
and each of the above are perfectly valid formats to dial in India only
But in case if the phone numbers which belong to other than your native country it has to be saved compulsory with Country Code or you won't be able to make a call.
ex.
a. +1732-757-2923 (US)
b. +97433-456-789 (Qatar)
So, the problem of matching contacts really occurs if the concerning contact is local/national
.
And that's where libphonenumber
comes into the picture. Using the library we can extract the phone number in actual national format belonging to that particular country.
Here's the trick. First pass the received number from sms as it is to the uri for matching in database. If it doesn't matches then extract the nationalized phone number
using libphonenumber and then again pass it as uri for matching. (Uri.encode(number)
)
It worked in my case.
I've used it in the following way.
public String getContactDisplayNameByNumber(String number) {
if (number != null && number.length() != 0) {
String name = lookupNumber(number);
if (!name.contains(number))
return name;
else {
TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String usersCountryISOCode = manager.getNetworkCountryIso();
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
try {
PhoneNumber phonenUmber = phoneUtil.parse(name,
usersCountryISOCode);
if (phoneUtil.isValidNumber(phonenUmber)) {
temp = phoneUtil
.getNationalSignificantNumber(phonenUmber);
name = lookupNumber(temp);
return name;
}
} catch (Exception e) {
e.printStackTrace();
}
return number;
}
} else {
return "Invalid Number";
}
}
private String lookupNumber(String number) {
uri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
name = number;
contentResolver = getContentResolver();
contactLookup = contentResolver.query(uri, columns, null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}
Hope it solves your problem.
For this type of problem, Google itself has provided libraries for us specially meant for Android phones. Don't use any weird code, take a look at Google's official library tested by experienced developers.
Use the libphonenumber library by Google meant for for parsing, formatting, storing and validating international phone numbers.
There are a lot of code snippets on the this landing page so I think there's no need to write any code here; you should be able to understand them from there.
http://code.google.com/p/libphonenumber/
They have given all the resources How to parse and How to match.
Features of this library that are relevant to you:
Parsing/formatting/validating phone numbers for all countries/regions of the world.
isNumberMatch - gets a confidence level on whether two numbers could be the same.
findNumbers - finds numbers in text input.
I am using this code for the same requirement and it returns a name by returning contactId from the same code. Using this, your problem will be solved.
public static String getContactDisplayNameByNumber(String number) {
Uri uri = Uri.withAppendedPath(
ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(number));
String name = "?";
ContentResolver contentResolver = context.getContentResolver();
Cursor contactLookup = contentResolver.query(uri, new String[] {
BaseColumns._ID, ContactsContract.PhoneLookup.DISPLAY_NAME },
null, null, null);
try {
if (contactLookup != null && contactLookup.getCount() > 0) {
contactLookup.moveToNext();
name = contactLookup.getString(contactLookup
.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
// String contactId =
// contactLookup.getString(contactLookup.getColumnIndex(BaseColumns._ID));
}
} finally {
if (contactLookup != null) {
contactLookup.close();
}
}
return name;
}