2013-05-13 114 views
9

我想存儲單元格的信號強度,我不喜歡這樣寫道:如何獲得當前的細胞信號強度?

private class GetRssi extends PhoneStateListener { 
    @Override 
    public void onSignalStrengthsChanged(SignalStrength signalStrength) { 
     super.onSignalStrengthsChanged(signalStrength); 
     Variables.signal = signalStrength.getGsmSignalStrength(); 


    } 

} 

好,但這隻有當它改變運行。我需要當前的信號強度。

有沒有一種方法來詢問當前的信號強度?

+1

如果您在應用程序啓動時註冊該監聽器,則您具有當前的信號強度。它不會改變,直到您再次被聽衆調用,您可以更新存儲強度的內部變量。 – Ryan 2013-05-13 19:35:16

+0

像Ryan說的...如果你跟蹤當前的信號強度,那麼你將永遠知道它目前是什麼! – Vorsprung 2013-05-13 19:51:38

回答

15

在API 17中添加了TelephonyManager中的getAllCellInfo()方法,這可能是一個很好的解決方案。使用示例:

TelephonyManager telephonyManager = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE); 
// for example value of first element 
CellInfoGsm cellinfogsm = (CellInfoGsm)telephonyManager.getAllCellInfo().get(0); 
CellSignalStrengthGsm cellSignalStrengthGsm = cellinfogsm.getCellSignalStrength(); 
cellSignalStrengthGsm.getDbm(); 
+1

好,但我使用較低的API,我仍然投票答覆謝謝。 – 2013-05-15 13:39:56

+2

是否有API級別8的等效代碼? – 2013-06-03 15:57:14

+7

只是擡頭:似乎有些設備(看着你三星)沒有正確實現getAllCellInfo()並且會返回null。 – 2013-11-19 13:29:48

11

CellSignalStrengthGsm()被添加在API級別17

CellSignalStrengthGsm()getDbm()會給你的信號強度dBm的

TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 

List<CellInfo> cellInfos = telephonyManager.getAllCellInfo(); //This will give info of all sims present inside your mobile 
if(cellInfos!=null){ 
    for (int i = 0 ; i<cellInfos.size(); i++){ 
      if (cellInfos.get(i).isRegistered()){ 
       if(cellInfos.get(i) instanceof CellInfoWcdma){ 
        CellInfoWcdma cellInfoWcdma = (CellInfoWcdma) telephonyManager.getAllCellInfo().get(0); 
        CellSignalStrengthWcdma cellSignalStrengthWcdma = cellInfoWcdma.getCellSignalStrength(); 
        strength = String.valueOf(cellSignalStrengthWcdma.getDbm()); 
       }else if(cellInfos.get(i) instanceof CellInfoGsm){ 
        CellInfoGsm cellInfogsm = (CellInfoGsm) telephonyManager.getAllCellInfo().get(0); 
        CellSignalStrengthGsm cellSignalStrengthGsm = cellInfogsm.getCellSignalStrength(); 
        strength = String.valueOf(cellSignalStrengthGsm.getDbm()); 
       }else if(cellInfos.get(i) instanceof CellInfoLte){ 
        CellInfoLte cellInfoLte = (CellInfoLte) telephonyManager.getAllCellInfo().get(0); 
        CellSignalStrengthLte cellSignalStrengthLte = cellInfoLte.getCellSignalStrength(); 
        strength = String.valueOf(cellSignalStrengthLte.getDbm()); 
       } 
      } 
     } 
     return strength; 
    } 

你可以學習。更多來自: https://developer.android.com/reference/android/telephony/CellInfo.html

CellInfoCdma,CellInfoGsm,CellInfoLte,CellInfoWcdma是CellInfo的子類。其中提供了與您的移動網絡相關的所有信息。

+1

完全適合我。我也爲'CellinfoCdma'添加了'if'分支。 – Minoru 2016-09-13 16:23:56