最近做的项目有一部分关于手机号码的操作,于是搜罗了一些资料,整了一个工具类。主要有以下三个功能:判断号码是否有效、获取号码运营商、获取号码归属地。
首先需要引入google开发的相关依赖或者下载对应的jar包
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>geocoder</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>6.3</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>prefixmapper</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>carrier</artifactId>
<version>1.5</version>
</dependency>
下面是工具类的源码:
import java.util.Locale;
import com.google.i18n.phonenumbers.PhoneNumberToCarrierMapper;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber.PhoneNumber;
import com.google.i18n.phonenumbers.geocoding.PhoneNumberOfflineGeocoder;
/**
*
* @ClassName: PhoneUtil
* @Description:手机号码归属地工具类
*/
public class PhoneUtil {
private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
/**
* 根据国家代码和手机号 判断手机号是否有效
* @param phoneNumber
* @param countryCode
* @return
*/
public static boolean checkPhoneNumber(String phoneNumber, String countryCode){
int ccode = StringUtils.obj2Int(countryCode);
long phone = StringUtils.toLong(phoneNumber);
PhoneNumber pn = new PhoneNumber();
pn.setCountryCode(ccode);
pn.setNationalNumber(phone);
return phoneNumberUtil.isValidNumber(pn);
}
/**
* 根据国家代码和手机号 判断手机运营商
* @param phoneNumber
* @param countryCode
* @return
*/
public static String getCarrier(String phoneNumber, String countryCode){
int ccode = StringUtils.obj2Int(countryCode);
long phone = StringUtils.toLong(phoneNumber);
PhoneNumber pn = new PhoneNumber();
pn.setCountryCode(ccode);
pn.setNationalNumber(phone);
//返回结果只有英文,自己转成成中文
String carrierEn = carrierMapper.getNameForNumber(pn, Locale.ENGLISH);
String carrierZh = "";
carrierZh += geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
switch (carrierEn) {
case "China Mobile":
carrierZh += "移动";
break;
case "China Unicom":
carrierZh += "联通";
break;
case "China Telecom":
carrierZh += "电信";
break;
default:
break;
}
return carrierZh;
}
/**
*
* @Description: 根据国家代码和手机号 手机归属地
* @param @param phoneNumber
* @param @param countryCode
* @param @return 参数
* @throws
*/
public static String getGeo(String phoneNumber, String countryCode){
int ccode = StringUtils.obj2Int(countryCode);
long phone = StringUtils.toLong(phoneNumber);
PhoneNumber pn = new PhoneNumber();
pn.setCountryCode(ccode);
pn.setNationalNumber(phone);
return geocoder.getDescriptionForNumber(pn, Locale.CHINESE);
}
/**
*
* @Title: getPhoneRegionCode
* @Description: 得到手机的归宿地编码
* @return String 返回类型
* @throws
*/
public static String getPhoneRegionCode(String phoneNumber, String countryCode){
String areaName=getGeo(phoneNumber,countryCode);
if(StringUtils.isEmpty(areaName)){
return "";
}
if(areaName.length()<3){
return "";
}
return areaName;
}
public static void main(String[] args) {
System.out.println(getPhoneRegionCode("18931234567","86"));
}
}
下面简单地跟踪了一下源码:
判断号码是否有效,核心方法是PhoneNumberUtil类的isValidNumberForRegion方法,PhoneNumberType是PhoneNumberUtil类中定义的一个枚举类,枚举出了各种号码的类型,例如FIXED_LINE(固定电话),MOBILE(手机号码)。getNumberTypeHelper方法返回的就是通过号码判断出的类型,如果不是UNKNOWN则认为是有效的号码。
public boolean isValidNumberForRegion(PhoneNumber number, String regionCode) {
int countryCode = number.getCountryCode();
PhoneMetadata metadata = getMetadataForRegionOrCallingCode(countryCode, regionCode);
if ((metadata == null) ||
(!REGION_CODE_FOR_NON_GEO_ENTITY.equals(regionCode) &&
countryCode != getCountryCodeForValidRegion(regionCode))) {
// Either the region code was invalid, or the country calling code for this number does not
// match that of the region code.
return false;
}
String nationalSignificantNumber = getNationalSignificantNumber(number);
return getNumberTypeHelper(nationalSignificantNumber, metadata) != PhoneNumberType.UNKNOWN;
}
在获取PhoneNumberOfflineGeocoder类对象时,会加载一个config文件,如下图所示,内容大致是将号码前缀与区域对应,这个文件是获取号码归属地的核心。
通过断点调试的方法,会调用PhonePrefixMap类的lookup方法回去的号码的描述(description)。
String lookup(long number) {
int numOfEntries = phonePrefixMapStorage.getNumOfEntries();
if (numOfEntries == 0) {
return null;
}
long phonePrefix = number;
int currentIndex = numOfEntries - 1;
SortedSet<Integer> currentSetOfLengths = phonePrefixMapStorage.getPossibleLengths();
while (currentSetOfLengths.size() > 0) {
Integer possibleLength = currentSetOfLengths.last();
String phonePrefixStr = String.valueOf(phonePrefix);
if (phonePrefixStr.length() > possibleLength) {
phonePrefix = Long.parseLong(phonePrefixStr.substring(0, possibleLength));
}
currentIndex = binarySearch(0, currentIndex, phonePrefix);
if (currentIndex < 0) {
return null;
}
int currentPrefix = phonePrefixMapStorage.getPrefix(currentIndex);
if (phonePrefix == currentPrefix) {
return phonePrefixMapStorage.getDescription(currentIndex);
}
currentSetOfLengths = currentSetOfLengths.headSet(possibleLength);
}
return null;
}
下面看看getDescription的实现方法,descriptionPool是一个字符串数组,通过计算出来的index获取对应的号码归属地。
public String getDescription(int index) {
int indexInDescriptionPool =
readWordFromBuffer(descriptionIndexes, descIndexSizeInBytes, index);
return descriptionPool[indexInDescriptionPool];
}
下图是获取归属地时的descriptionPool详情
获取运营商的方法与获取归属地最终调用的方法相同,只是查询运营商时传入的参数是英文,而查询归属地时传入的参数是中文,下图是查询运营商时的getDescription情况,可以看到descriptionPool现在的值是运营商。