2013-05-13 19 views
9

chciałbym przechowywać siłę sygnału komórkowego, a robię to tak:Jak uzyskać aktualną moc sygnału komórki?

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


    } 

} 

porządku, ale to działa tylko jeśli to się nie zmienia. Potrzebuję obecnej siły sygnału.

Czy istnieje metoda, aby zapytać o aktualną siłę sygnału?

+1

Jeśli zarejestrujesz tego słuchacza, gdy uruchomi się Twoja aplikacja, będziesz miał aktualną siłę sygnału. Nie zmieni się, dopóki nie zostaniesz ponownie wywołany przez słuchacza, w którym momencie możesz zaktualizować wewnętrzną zmienną przechowującą siłę. – Ryan

+0

Jak mówi Ryan ... jeśli będziesz śledzić aktualną siłę sygnału, zawsze będziesz wiedział, co to jest! – Vorsprung

Odpowiedz

15

Istnieje metoda getAllCellInfo() w Menedżerze telefonii dodana w API 17, która może być dobrym rozwiązaniem. Przykład zastosowania:

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

Dobrze, ale używam niższej api, wciąż głosowałem za odpowiedź dzięki. –

+2

Czy istnieje kod odpowiadający poziomowi API 8? –

+7

Po prostu heads up: Wygląda na to, że niektóre urządzenia (patrząc na ciebie Samsunga) nie poprawnie implementują getAllCellInfo() i zwrócą wartość null. –

11

CellSignalStrengthGsm() dodaje Dodane w poziomie API 17

CellSignalStrengthGsm() getDbm() daje siłę sygnału w 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; 
    } 

można nauczyć. więcej od: https://developer.android.com/reference/android/telephony/CellInfo.html

CellInfoCdma, CellInfoGsm, CellInfoLte, CellInfoWcdma to podklasy CellInfo. Który daje wszystkie informacje związane z twoją siecią komórkową.

+1

Sprawdził się doskonale. Właśnie dodałem gałąź 'if' dla' CellinfoCdma'. – Minoru

Powiązane problemy