简体   繁体   中英

What is the best way to match two format of phone numbers in android SDK?

What should be the best method to match phone numbers in situation where the format of phone number will be a problem?

I am looking for a standards rich best practices way of doing this. I have users type in a phone number and save to the SQLite DB. Later on a point of time, when a phone calls comes, I get the incomingNumber through ITelephony. I have to match the value in db with the incomingNumber and take an action. The formats of these numbers may differ greatly and may cause problems.

To get around this problem, I am using regex right now to eliminate formatting characters and leave only digits to get a proper match. This is working fine but I wanted to know if someone's got a better way to do it, or if Android has an API Method to match two phone numbers.

Thanks for your interest

EDIT: The code that I am using is:

    private boolean matchPhoneNumbers(String numberA, String numberB)
    {           
        //Remove everything that's not a number
        numberA=numberA.replaceAll("[^0-9]", "");
        numberB=numberB.replaceAll("[^0-9]", "");
        //Get the minimum length of one of the strings
        int length=Math.min(numberA.length(),numberB.length());
        numberA=numberA.substring((numberA.length()-length));
        numberB=numberB.substring((numberB.length()-length));  
        //check the difference
        return numberA.equals(numberB);
    }

如果这是Android 4,则可能只需要内置的libphonenumber方法。

遍历每个字符并除去小于0或大于9的任何字符(易于循环),然后检查右边的10个字符是否相同。

There is a helper-function available for this since Api level 1:

boolean PhoneNumberUtils.compare(numberA, numberB);

I happened to find this in parallel to this post and think it is worth mentioning here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM