2013-05-13 26 views
9

Tôi muốn để lưu trữ các cường độ tín hiệu tế bào, và tôi làm điều đó như thế này:Làm cách nào để nhận cường độ tín hiệu tế bào hiện tại?

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


    } 

} 

rồi nhưng điều này chỉ chạy nếu nó thay đổi. Tôi cần cường độ tín hiệu hiện tại.

Có phương pháp nào để yêu cầu cường độ tín hiệu hiện tại không?

+1

Nếu bạn đăng ký người nghe này khi ứng dụng của bạn bắt đầu thì bạn có cường độ tín hiệu hiện tại. Nó sẽ không thay đổi cho đến khi bạn được gọi bởi người nghe một lần nữa tại thời điểm đó bạn có thể cập nhật biến nội bộ của bạn để lưu trữ sức mạnh. – Ryan

+0

Giống như Ryan nói ... nếu bạn theo dõi cường độ tín hiệu hiện tại thì bạn sẽ luôn biết nó hiện tại là gì! – Vorsprung

Trả lời

15

Có phương thức getAllCellInfo() trong TelephonyManager được thêm vào API 17 có thể là giải pháp tốt. Ví dụ về sử dụng:

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

Tốt nhất nhưng tôi sử dụng api thấp hơn, tôi vẫn bình chọn cho câu trả lời cảm ơn. –

+2

Có mã tương đương cho API cấp 8 không? –

+7

Chỉ cần một đầu lên: Có vẻ như một số thiết bị (nhìn bạn Samsung) không thực hiện đúng getAllCellInfo() và sẽ trả về null. –

11

CellSignalStrengthGsm() được bổ sung thêm trong mức API 17

CellSignalStrengthGsm() getDbm() sẽ cung cấp cho bạn sức mạnh tín hiệu như 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; 
    } 

Bạn có thể học. thêm từ: https://developer.android.com/reference/android/telephony/CellInfo.html

CellInfoCdma, CellInfoGsm, CellInfoLte, CellInfoWcdma là các lớp con của CellInfo. Cung cấp tất cả thông tin liên quan đến mạng di động của bạn.

+1

Làm việc hoàn hảo cho tôi. Tôi vừa thêm nhánh 'if' cho' CellinfoCdma' nữa. – Minoru

Các vấn đề liên quan