2014-07-16 69 views
1

我想在dbm中顯示信號強度。但用我的代碼,我總是變成結果-1。但是當我在設置中看到的信號強度是-81 .. 你能找到一個錯誤嗎?謝謝!在dbm中獲得SignalStrength

public class MainActivity extends Activity implements OnClickListener { 
    TextView dateAndTimeLabel; 
    private int signalDBM = 0; 

    public class GetParams extends PhoneStateListener { 
     @Override 
     public void onSignalStrengthsChanged(SignalStrength signalStrength) { 
      super.onSignalStrengthsChanged(signalStrength); 
      signalDBM = signalStrength.getEvdoDbm(); 
     } 
    } 

    @Override 
    public void onCreate(Bundle icicle) { 
     super.onCreate(icicle); 
     setContentView(R.layout.main); 

     GetParams listener = new GetParams(); 
     TelephonyManager TelManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE); 
     TelManager.listen(listener ,PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); 

     Button btn = (Button)findViewById(R.id.start); 
     btn.setOnClickListener(this);    
    } 

    public void onClick(View v) { 
     Toast.makeText(this, "CDMA signal strength is " + this.signalDBM, Toast.LENGTH_SHORT).show();    
    } 

    /* Create options menu etc*/ 
} 
+0

任何人都可以幫助我嗎? – user3379235

回答

0

試試這個:

List<CellInfo> cellInfoList = tm.getAllCellInfo(); 
      //Checking if list values are not null 
      if (cellInfoList != null) { 
       for (final CellInfo info : cellInfoList) { 
        if (info instanceof CellInfoGsm) { 
         //GSM Network 
         CellSignalStrengthGsm cellSignalStrength = ((CellInfoGsm)info).getCellSignalStrength(); 
         dBmlevel = cellSignalStrength.getDbm(); 
         asulevel = cellSignalStrength.getAsuLevel(); 
        } 
        else if (info instanceof CellInfoCdma) { 
         //CDMA Network 
         CellSignalStrengthCdma cellSignalStrength = ((CellInfoCdma)info).getCellSignalStrength(); 
         dBmlevel = cellSignalStrength.getDbm(); 
         asulevel = cellSignalStrength.getAsuLevel(); 
        } 
        else if (info instanceof CellInfoLte) { 
         //LTE Network 
         CellSignalStrengthLte cellSignalStrength = ((CellInfoLte)info).getCellSignalStrength(); 
         dBmlevel = cellSignalStrength.getDbm(); 
         asulevel = cellSignalStrength.getAsuLevel(); 
        } 
        else if (info instanceof CellInfoWcdma) { 
         //WCDMA Network 
         CellSignalStrengthWcdma cellSignalStrength = ((CellInfoWcdma)info).getCellSignalStrength(); 
         dBmlevel = cellSignalStrength.getDbm(); 
         asulevel = cellSignalStrength.getAsuLevel(); 
        } 
        else{ 
         //Developed as a Cordova plugin, that's why I'm using callbackContext 
         callbackContext.error("Unknown type of cell signal."); 
        } 
       } 
      } 

如果你的手機是三星,它會返回0 dBm時,0 ASU。如果沒有,那麼它應該工作(它在我的Xperia M2 Aqua中工作)。