我可以从来电或短信中获取电话号码。不幸的是,在短信的情况下可能会有国家代码。因此,基本上我需要获取纯电话号码,而不带国家代码,以便与联系人中现有的号码进行比较。
我可以从来电或短信中获取电话号码。不幸的是,在短信的情况下可能会有国家代码。因此,基本上我需要获取纯电话号码,而不带国家代码,以便与联系人中现有的号码进行比较。
如果你想比较电话号码,你可以始终使用
PhoneNumberUtils.compare(number1, number2);
或者PhoneNumberUtils.compare(context, number1, number2);
那么你就不必担心国家代码,它只会比较反向顺序的数字并查看它们是否匹配(至少足够用于来电显示的目的)。
快速未经测试的方法(据我所知,电话号码有10位数字):
// As I said, AFAIK phone numbers have 10 digits... (at least here in Mexico this is true)
int digits = 10;
// the char + is always at first.
int plus_sign_pos = 0;
// Always send the number to this function to remove the first n digits (+1,+52, +520, etc)
private String removeCountryCode(String number) {
if (hasCountryCode(number)) {
// +52 for MEX +526441122345, 13-10 = 3, so we need to remove 3 characters
int country_digits = number.length() - digits;
number = number.substring(country_digits);
}
return number;
}
// Every country code starts with + right?
private boolean hasCountryCode(String number) {
return number.charAt(plus_sign_pos) == '+'; // Didn't String had contains() method?...
}
然后你只需要调用这些函数