libphonenumber是用于解析、格式化、存储和校验电话号码的Java、C++或JavaScript类库。2010年,libphonenumber的Java实现首次以开源形式发布。在2014年,libphonenumber成为Debian发行版的一部分。
利用开源项目libphonenumber的资源:https://github.com/googlei18n/libphonenumber
根据项目的构建方式,将libphonenumber所需要的jar包加载
compile group: 'com.googlecode.libphonenumber', name: 'libphonenumber', version: '8.9.2'
compile group: 'com.googlecode.libphonenumber', name: 'carrier', version: '1.81'
compile group: 'com.googlecode.libphonenumber', name: 'prefixmapper', version: '2.91'
compile group: 'com.googlecode.libphonenumber', name: 'geocoder', version: '2.91'
获取手机号归属地
/**
* @description: PhoneUtil
* @author: wangshuai
* @create: 2018-04-04 14:02
* @version: 1.0
* @remark: 获取手机号相关信息
**/
public class PhoneUtil {
private static PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper.getInstance();
private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder.getInstance();
private static String LANGUAGE ="CN";
/**
* 根据国家代码和手机号 判断手机运营商
* @param phoneNumber
* @return
*/
public static String GetCarrier(String phoneNumber) throws NumberParseException {
Phonenumber.PhoneNumber referencePhonenumber = new Phonenumber.PhoneNumber();
referencePhonenumber = phoneNumberUtil.parse(phoneNumber,LANGUAGE );
//返回结果只有英文,自己转成成中文
String carrierEn = carrierMapper.getNameForNumber(referencePhonenumber, Locale.ENGLISH);
String carrierZh = "";
carrierZh += geocoder.getDescriptionForNumber(referencePhonenumber, 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 @return 参数
* @throws
*/
public static String getGeo(String phoneNumber){
Phonenumber.PhoneNumber referencePhonenumber = null;
try {
referencePhonenumber = phoneNumberUtil.parse(phoneNumber, LANGUAGE);
} catch (NumberParseException e) {
e.printStackTrace();
}
//手机号码归属城市 referenceRegion
return geocoder.getDescriptionForNumber(referencePhonenumber,Locale.CHINA);
}
}
参考网站https://blog.csdn.net/fairytall/article/details/46860593