Java手機號碼工具類(判斷運營商、獲取歸屬地)以及簡要的原理跟蹤

最近做的項目有一部分關於手機號碼的操作,於是蒐羅了一些資料,整了一個工具類。主要有以下三個功能:判斷號碼是否有效、獲取號碼運營商、獲取號碼歸屬地。

首先需要引入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現在的值是運營商。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章