6

我試圖得到以下數據:安卓得到的CellID和RSS用於基站和Neigboring細胞

  • 基站:的CellID和RSS(識別哪一個是基站)
  • 對於所有neigbouring站:CellID和RSS

有各種API,它看起來像我不得不使用不同的API TelephonyManager和PhoneStateListener。我有點困惑,因爲我認爲這應該在一個界面中可用。另外我認爲應該可以輪詢當前基站的CellID,而不必監聽狀態改變來確定int,因爲鄰居小區站cal也可以從電話管理器輪詢。

你能告訴我如何獲得上面指定的數據嗎?

回答

6

新skool-way:API 17更好更清潔,但仍然太新。

List<CellInfo> cellInfos = (List<CellInfo>) this.telephonyManager.getAllCellInfo(); 

for(CellInfo cellInfo : cellInfos) 
{ 
    CellInfoGsm cellInfoGsm = (CellInfoGsm) cellInfo; 

    CellIdentityGsm cellIdentity = cellInfoGsm.getCellIdentity(); 
    CellSignalStrengthGsm cellSignalStrengthGsm = cellInfoGsm.getCellSignalStrength(); 

    Log.d("cell", "registered: "+cellInfoGsm.isRegistered()); 
    Log.d("cell", cellIdentity.toString());   
    Log.d("cell", cellSignalStrengthGsm.toString()); 
} 

舊的API沒有提供非常令人滿意的解決方案。但這裏的老斯庫爾方式:

this.telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE); 
this.phoneStateListener = setupPhoneStateListener(); 
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CELL_LOCATION); 
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_DATA_CONNECTION_STATE); 
this.telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 

// This part is used to listen for properties of the neighboring cells 
List<NeighboringCellInfo> neighboringCellInfos = this.telephonyManager.getNeighboringCellInfo(); 
for(NeighboringCellInfo neighboringCellInfo : neighboringCellInfos) 
{ 
    neighboringCellInfo.getCid(); 
    neighboringCellInfo.getLac(); 
    neighboringCellInfo.getPsc(); 
    neighboringCellInfo.getNetworkType(); 
    neighboringCellInfo.getRssi(); 

    Log.d("cellp",neighboringCellInfo.toString()); 
} 

public PhoneStateListener setupPhoneStateListener() 
{ 
    return new PhoneStateListener() { 

     /** Callback invoked when device cell location changes. */ 
     @SuppressLint("NewApi") 
     public void onCellLocationChanged(CellLocation location) 
     { 
      GsmCellLocation gsmCellLocation = (GsmCellLocation) location; 
      gsmCellLocation.getCid(); 
      gsmCellLocation.getLac(); 
      gsmCellLocation.getPsc(); 

      Log.d("cellp", "registered: "+gsmCellLocation.toString()); 
     } 

     /** invoked when data connection state changes (only way to get the network type) */ 
     public void onDataConnectionStateChanged(int state, int networkType) 
     { 
      Log.d("cellp", "registered: "+networkType); 
     } 

     /** Callback invoked when network signal strengths changes. */ 
     public void onSignalStrengthsChanged(SignalStrength signalStrength) 
     { 
      Log.d("cellp", "registered: "+signalStrength.getGsmSignalStrength()); 
     } 

    }; 
} 
  • 不要忘記設置所有必要的權限 這種解決方案的問題是,我沒有得到任何相鄰小區信息沉綿我的設置:

    使用許可權的android:NAME =「android.permission.ACCESS_COARSE_UPDATES」

(請注意,我用的是三星手機,這是一個已知的問題,三星手機唐「T支持相鄰小區的列表)

+1

我得到一個'android.telephony.CellInfoWcdma不能轉換到android.telephony.CellInfoGsm'上'CellInfoGsm cellInfoGsm =(CellInfoGsm)cellInfo;' – msysmilu 2015-09-02 16:35:27

+0

代碼可以是不是100%在編寫代碼時,API太新了,無法在我的手機上運行。如果您有解決方案,請發佈新問題和/或提出修改建議。 – ndrizza 2015-09-12 18:08:26

+0

抓取這些數據是否合法? – heximal 2016-09-21 23:11:37