在googleAPI裡提供了基站信息的獲取類TelephonyManager,通過其方法getCellLocation得到CellLocation即可獲取到基站相關信息
但CellLocation是個抽象類,所以在具體使用時需要判斷接入的網絡制式來用其子類CdmaCellLocation或GsmCellLocation 來強轉
CdmaCellLocation對應CDMA網,GsmCellLocation對應GSM網
三大網絡運營商的網絡制式對應如下:
移動2G 網 --> GSM
移動3G 網 --> TD-SCDMA
電信2G 網 --> CDMA
電信3G 網 --> CDMA2000
聯通2G 網 --> GSM
聯通3G 網 --> WCDMA
由此可見移動,聯通2G 網都可使用GsmCellLocation
電信2G,3G網則使用CdmaCellLocation
那麼移動3G和聯通3G又當如何
其實經本人親測,移動3G網也可使用GsmCellLocation,聽說是TD-SCDMA衍生於GSM,具體原因咱也不用糾結了,反正能用就是了
而聯通的WCDMA據說也可使用GsmCellLocation,那姑且就是這樣吧,有條件的童鞋試一試吧。
對於網絡制式的判斷調用TelephonyManager.getNetworkType()可有多種情況,如下:
NETWORK_TYPE_UNKNOWN
NETWORK_TYPE_GPRS
NETWORK_TYPE_EDGE
NETWORK_TYPE_UMTS
NETWORK_TYPE_HSDPA
NETWORK_TYPE_HSUPA
NETWORK_TYPE_HSPA
NETWORK_TYPE_CDMA
NETWORK_TYPE_EVDO_0
NETWORK_TYPE_EVDO_A
NETWORK_TYPE_EVDO_B
NETWORK_TYPE_1xRTT
NETWORK_TYPE_IDEN
NETWORK_TYPE_LTE
NETWORK_TYPE_EHRPD
通過對網絡類型判斷後獲取對應基站信息代碼片段如下:
- public static ArrayList<CellIDInfo> getCellIDInfo(Context context) throws Exception{
-
- TelephonyManager manager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
-
- ArrayList<CellIDInfo> CellID = new ArrayList<CellIDInfo>();
- CellIDInfo currentCell = new CellIDInfo();
-
- int type = manager.getNetworkType();
- Log.d(TAG, "getCellIDInfo--> NetworkType = " + type);
- int phoneType = manager.getPhoneType();
- Log.d(TAG, "getCellIDInfo--> phoneType = " + phoneType);
-
- if (type == TelephonyManager.NETWORK_TYPE_GPRS // GSM網
- || type == TelephonyManager.NETWORK_TYPE_EDGE
- || type == TelephonyManager.NETWORK_TYPE_HSDPA)
- {
- GsmCellLocation gsm = ((GsmCellLocation) manager.getCellLocation());
- if (gsm == null)
- {
- Log.e(TAG, "GsmCellLocation is null!!!");
- return null;
- }
-
-
- int lac = gsm.getLac();
- String mcc = manager.getNetworkOperator().substring(0, 3);
- String mnc = manager.getNetworkOperator().substring(3, 5);
- int cid = gsm.getCid();
-
- currentCell.cellId = gsm.getCid();
- currentCell.mobileCountryCode = mcc;
- currentCell.mobileNetworkCode = mnc;
- currentCell.locationAreaCode = lac;
-
- currentCell.radioType = "gsm";
-
- CellID.add(currentCell);
-
- // 獲得鄰近基站信息
- List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
- int size = list.size();
- for (int i = 0; i < size; i++) {
-
- CellIDInfo info = new CellIDInfo();
- info.cellId = list.get(i).getCid();
- info.mobileCountryCode = mcc;
- info.mobileNetworkCode = mnc;
- info.locationAreaCode = lac;
-
- CellID.add(info);
- }
-
- }else if (type == TelephonyManager.NETWORK_TYPE_CDMA // 電信cdma網
- || type == TelephonyManager.NETWORK_TYPE_1xRTT
- || type == TelephonyManager.NETWORK_TYPE_EVDO_0
- || type == TelephonyManager.NETWORK_TYPE_EVDO_A)
- {
-
- CdmaCellLocation cdma = (CdmaCellLocation) manager.getCellLocation();
- if (cdma == null)
- {
- Log.e(TAG, "CdmaCellLocation is null!!!");
- return null;
- }
-
- int lac = cdma.getNetworkId();
- String mcc = manager.getNetworkOperator().substring(0, 3);
- String mnc = String.valueOf(cdma.getSystemId());
- int cid = cdma.getBaseStationId();
-
- currentCell.cellId = cid;
- currentCell.mobileCountryCode = mcc;
- currentCell.mobileNetworkCode = mnc;
- currentCell.locationAreaCode = lac;
-
- currentCell.radioType = "cdma";
-
- CellID.add(currentCell);
-
- // 獲得鄰近基站信息
- List<NeighboringCellInfo> list = manager.getNeighboringCellInfo();
- int size = list.size();
- for (int i = 0; i < size; i++) {
-
- CellIDInfo info = new CellIDInfo();
- info.cellId = list.get(i).getCid();
- info.mobileCountryCode = mcc;
- info.mobileNetworkCode = mnc;
- info.locationAreaCode = lac;
-
- CellID.add(info);
- }
- }
-
- return CellID;
-
- }
從GOOGLE的API文檔裡總共有14鐘網絡類型,這裡只羅列了其中7種,其他的主要是本人也不太清楚其對應到的網絡制式是怎樣的