Android Phone Number Comparison

I need to compare two phone numbers to determine if they are from the same sender / receiver. The user can send a message to the contact, and this contact can reply.

The answer usually comes +[country-code][area-code-if-any][and-then-the-actual-number] . For example, +94 71 2593276 for the phone number of Sri Lanka.

And when the user sends a message, he usually enters in the format (for the example above) 0712593276 (suppose he is also in Sri Lanka).

So what I need, I need to check if these two numbers match. This app will be used internationally. Therefore, I can’t just replace the first 2 digits with 0 (then this will be a problem for countries like the USA). Is there any way to do this in Java or Android?

Thanks.

+7
source share
4 answers

Android has a nice PhoneNumberUtils, and I think you're looking for:

  public static boolean compare (String a, String b) 

look at: http://developer.android.com/reference/android/telephony/PhoneNumberUtils.html

usage should look like this:

 String phone1 String phone2 if (PhoneNumberUtils.compare(phone1, phone2)) { //they are the same do whatever you want! } 
+12
source

android.telephony. PhoneNumberUtils android.telephony. PhoneNumberUtils class provides almost all the necessary functions for working with phone numbers and standards.

In your case, the solution is PhoneNumberUtils. compare(Context context, String number1, String number2) PhoneNumberUtils. compare(Context context, String number1, String number2) or else PhoneNumberUtils. compare(String number1, String number2) PhoneNumberUtils. compare(String number1, String number2) . The first checks the resource to determine whether to use a strict or free comparison algorithm, so the best choice in most cases.

 PhoneNumberUtils.compare("0712593276", "+94712593276") // always true PhoneNumberUtils.compare("0712593276", "+44712593276") // always true // true or false depends on the context PhoneNumberUtils.compare(context, "0712593276", "+94712593276") 

Take a look at the official documentation . And the source code .

+5
source

How to check if a number is a substring of a receiver number?

For example, let's say my Brazilian number is 888-777-666, and yours is 111-222-333. To call you, I need to dial additional numbers for international calls. Let's say I need to add 9999 + your_number, the result is 9999111222333.

If RawNumber.substring(your_number) returns true , I can say that I'm calling you.

+2
source

just apply your logic to remove () and - and follow PhoneNumberUtils

-one
source

All Articles