JAVA根据⼿机号获取省份和地区
JAVA根据⼿机号获取省份地区运营商
1.封装返回对象
/***
* @Auther:pmh 879564936
* @Version:1.0
*/
public class PhoneModel {
private String provinceName;//省份
private String cityName;//城市
private String carrier;//运营商
public String getProvinceName(){
return provinceName;
}
public void setProvinceName(String provinceName){
this.provinceName = provinceName;
}
public String getCityName(){
return cityName;
}
public void setCityName(String cityName){
this.cityName = cityName;
}
public String getCarrier(){
return carrier;
}
public void setCarrier(String carrier){
this.carrier = carrier;
}
}
2.⼯具类
le.i18n.phonenumbers.PhoneNumberToCarrierMapper;
le.i18n.phonenumbers.Phonenumber;
le.ding.PhoneNumberOfflineGeocoder;
import java.util.Locale;
public class PhoneNumberUtil {
//出直辖市
private final static String[] MUNICIPALITY ={"北京市","天津市","上海市","重庆市"};
//出⾃治区
private final static String[] AUTONOMOUS_REGION ={"新疆","内蒙古","西藏","宁夏","⼴西"};
private le.i18n.phonenumbers.PhoneNumberUtil phoneNumberUtil = le.i18n.phonenumbers.PhoneNumberUtil .getInstance();
//提供与电话号码相关的运营商信息
private static PhoneNumberToCarrierMapper carrierMapper = PhoneNumberToCarrierMapper
.getInstance();
// 提供与电话号码有关的地理信息
private static PhoneNumberOfflineGeocoder geocoder = PhoneNumberOfflineGeocoder
.getInstance();
//中国⼤陆区号
private final static int COUNTRY_CODE =86;
/**
* 根据⼿机号判断⼿机号是否有效
* 根据⼿机号判断⼿机号是否有效
* @param phoneNumber
* @return true-有效 false-⽆效
*/
public static boolean checkPhoneNumber(String phoneNumber){
long phone = Long.parseLong(phoneNumber);
Phonenumber.PhoneNumber pn =new Phonenumber.PhoneNumber();
pn.setCountryCode(COUNTRY_CODE);
pn.setNationalNumber(phone);
return phoneNumberUtil.isValidNumber(pn);
}
/**
* 根据⼿机号判断⼿机运营商
* @param phoneNumber
*/
public static String getCarrier(String phoneNumber){
long phone = Long.parseLong(phoneNumber);
Phonenumber.PhoneNumber pn =new Phonenumber.PhoneNumber();
pn.setCountryCode(COUNTRY_CODE);
pn.setNationalNumber(phone);
// 返回结果只有英⽂,⾃⼰转成成中⽂
java switch case stringString carrierEn = NameForNumber(pn, Locale.ENGLISH); String carrierZh ="";
switch(carrierEn){
case"China Mobile":
carrierZh +="移动";
break;
case"China Unicom":
carrierZh +="联通";
break;
case"China Telecom":
carrierZh +="电信";
break;
default:
break;
}
return carrierZh;
}
/
**
* 根据⼿机号获取⼿机归属地
*/
public static String getGeo(String phoneNumber){
long phone = Long.parseLong(phoneNumber);
Phonenumber.PhoneNumber pn =new Phonenumber.PhoneNumber();
pn.setCountryCode(COUNTRY_CODE);
pn.setNationalNumber(phone);
DescriptionForNumber(pn, Locale.CHINESE);
}
/**
* 根据⼿机号获取封裝信息
*/
public static PhoneModel getPhoneModel(String phoneNumber){
if(checkPhoneNumber(phoneNumber)){
String geo =getGeo(phoneNumber);
PhoneModel phoneModel =new PhoneModel();
String carrier =getCarrier(phoneNumber);
phoneModel.setCarrier(carrier);
// 直辖市
// 直辖市
for(String val : MUNICIPALITY){
if(geo.equals(val)){
phoneModel.place("市",""));
phoneModel.setCityName(val);
return phoneModel;
}
}
// ⾃治区
for(String val : AUTONOMOUS_REGION){
if(geo.startsWith(val)){
phoneModel.setProvinceName(val);
phoneModel.place(val,""));
return phoneModel;
}
}
// 其它
String[] splitArr = geo.split("省");
if(splitArr != null && splitArr.length ==2){
phoneModel.setProvinceName(splitArr[0]);
phoneModel.setCityName(splitArr[1]);
return phoneModel;
}
}
return null;
}
}
3.需要引⼊的pom配置
<!--根据⼿机号查询地区 -->
<dependency>
<groupId&lecode.libphonenumber</groupId>
<artifactId>geocoder</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId&lecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>6.3</version>
</dependency>
<dependency>
<groupId&lecode.libphonenumber</groupId>
<artifactId>prefixmapper</artifactId>
<version>2.15</version>
</dependency>
<dependency>
<groupId&lecode.libphonenumber</groupId>
<artifactId>carrier</artifactId>
<version>1.5</version>
</dependency>
<!--根据⼿机号查地区end-->
4.测试
public static void main(String[] args){
PhoneModel phoneModel = PhoneModel("159XXXXXX");
if(null != phoneModel){//可能会有空的情况
System.out.ProvinceName()+" "+ CityName());
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论