以dbm获得信号强度

时间:2014-07-16 12:39:02

标签: android signal-strength dbm

我想在dbm中显示信号强度。但是使用我的代码我总是成为结果-1。但当我看到设置中的信号强度时,它在-81左右。 你能找到错误吗?谢谢!

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*/
}

1 个答案:

答案 0 :(得分:1)

试试这个:

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中工作)。