2013-05-13 2 views
9

je voudrais stocker la puissance du signal cellulaire, et je le fais comme ceci:Comment obtenir la puissance actuelle du signal cellulaire?

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


    } 

} 

D'accord, mais cela ne fonctionne que si elle change. J'ai besoin de la force du signal actuel.

Existe-t-il une méthode pour demander simplement la force du signal actuel?

+1

Si vous enregistrez ce programme d'écoute lorsque votre application démarre, vous disposez de la force du signal en cours. Il ne changera pas jusqu'à ce que vous soyez à nouveau appelé par l'auditeur, auquel cas vous pourrez mettre à jour votre variable interne en stockant la force. – Ryan

+0

Comme le dit Ryan ... si vous gardez la trace de la force du signal actuel, vous saurez toujours ce qu'il est actuellement! – Vorsprung

Répondre

15

Il existe une méthode getAllCellInfo() dans TelephonyManager ajoutée dans l'API 17 qui pourrait être une bonne solution. Exemple d'utilisation:

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

Un bon mais j'utilise api inférieur, j'ai encore voté pour la réponse merci. –

+2

Existe-t-il un code équivalent pour le niveau API 8? –

+7

Juste un heads up: Il semble que certains appareils (en regardant Samsung) n'implémentent pas correctement getAllCellInfo() et retourneront null. –

11

CellSignalStrengthGsm() est ajouté Ajouté au niveau de l'API 17

CellSignalStrengthGsm() getDbm() vous donnera la puissance du signal en 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; 
    } 

Vous pouvez apprendre. more from: https://developer.android.com/reference/android/telephony/CellInfo.html

CellInfoCdma, CellInfoGsm, CellInfoLte, CellInfoWcdma sont les sous-classes de CellInfo. Ce qui donne toutes les informations relatives à votre réseau mobile.

+1

A travaillé parfaitement pour moi. J'ai juste ajouté la branche 'if' pour le' CellinfoCdma' aussi. – Minoru

Questions connexes