![](/img/trans.png)
[英]What is the correct way to update a list of contact phone numbers in Android?
[英]What is the best way to match two format of phone numbers in android SDK?
在电话号码格式有问题的情况下,匹配电话号码的最佳方法应该是什么?
我正在寻找一种标准丰富的最佳实践方法。 我让用户输入电话号码并保存到SQLite数据库。 稍后,当有电话打来时,我通过ITelephony获得了来话号码。 我必须将db中的值与入库号进行匹配并采取措施。 这些数字的格式可能相差很大,并可能导致问题。
为了解决这个问题,我现在使用正则表达式消除格式字符,仅保留数字以获取正确的匹配。 这工作正常,但我想知道是否有人可以采用更好的方法,或者Android是否具有可以匹配两个电话号码的API方法。
谢谢你的关注
编辑:我正在使用的代码是:
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个字符是否相同。
从Api级别1开始,有一个辅助功能可用:
boolean PhoneNumberUtils.compare(numberA, numberB);
我碰巧发现了与此帖子平行的帖子,并认为在这里值得一提。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.