本文介绍了在dbm中获取SignalStrength的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to show the the signal strength in dbm. But with my code i always become the result -1. But when i see the signal strength in the settings it's around -81..Can you find a mistake? thank you!

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

推荐答案

尝试一下:

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中起作用).

If your cellphone is Samsung, it'll return 0 dBm, 0 asu. If not, then it should work (it worked in my Xperia M2 Aqua).

这篇关于在dbm中获取SignalStrength的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-03 15:48