Get phone number without country code for the purpose of comparing numbers

Alin picture Alin · Jul 25, 2011 · Viewed 10.3k times · Source

I can obtain the phone number from an incoming call or from a sms message. unfortunately, in case of the SMS there might be the country code in it. So, basically I need to obtain the plain phone number, without country code, in order to compare it with existing numbers in Contacts.

Answer

Tomas picture Tomas · Jul 25, 2011

If you want to compare phone numbers you can always use the

PhoneNumberUtils.compare(number1, number2);

or

PhoneNumberUtils.compare(context, number1, number2);

Then you don't have to worry about the country code, it will just compare the numbers from the reversed order and see if they match (enough for callerID purposes at least).