2 baidu 34826909 baidu_34826909 于 2016.09.07 15:24 提问

android获取基站信号强度?

1.这个方法中toast的是基站信号强度吗?手机上面显示的信号强度和基站信号强度一样吗?
想获得连接基站的信号强度这样写对吗?
2.下面代码打印时输出结果:CellInfoGsm:{mRegistered=YES mTimeStampType=oem_ril mTimeStamp=327578425571078ns CellIdentityGsm:{ mMcc=460 mMnc=0 mLac=20978 mCid=64749} CellSignalStrengthGsm: ss=22 ber=99}
这里的ss,和ber是什么意思啊?

  1. List infos = telephonyManager.getAllCellInfo(); // List infos = telephonyManager.getNeighboringCellInfo();这个不能用了,返回值一直是infos.size为0,求助 ```
 private class MyPhoneStateListener extends PhoneStateListener
    {
        /* Get the Signal strength from the provider, each tiome there is an update  从得到的信号强度,每个tiome供应商有更新*/
        @Override

        public void onSignalStrengthsChanged(SignalStrength signalStrength)
        {
            super.onSignalStrengthsChanged(signalStrength);
            if (signalStrength.getGsmSignalStrength() != 99) {
                Toast.makeText(getApplicationContext(),
                        "Go to Firstdroid!!! GSM Cinr = " + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113) + "dbM", Toast.LENGTH_SHORT).show();
                System.out.println("****" + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113));
            }
        }
    }

代码:

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "GSMCellLocationActivity";
    TelephonyManager telephonyManager;
    MyPhoneStateListener MyListener;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final TextView textView1 = (TextView) findViewById(R.id.text1);
        final TextView textView2 = (TextView) findViewById(R.id.text2);
        Button button= (Button) findViewById(R.id.button1);
         telephonyManager= (TelephonyManager) MainActivity.this.getSystemService(Context.TELEPHONY_SERVICE);
        MyListener = new MyPhoneStateListener();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String operator = telephonyManager.getNetworkOperator();
                /**通过operator获取 MCC 和MNC */
                int mcc = Integer.parseInt(operator.substring(0, 3));
                int mnc = Integer.parseInt(operator.substring(3));
                GsmCellLocation location = (GsmCellLocation) telephonyManager.getCellLocation();
                /**通过GsmCellLocation获取中国移动和联通 LAC 和cellID */
                int lac = location.getLac();
                int cellid = location.getCid();
                System.out.println("**"+mcc+"A"+mnc+"A"+lac+"A"+cellid);
                textView1.setText("国家编号:"+mcc+"运营商编号:"+mnc+"LAC:"+lac+"CellID:"+cellid);
                List<CellInfo> infos = telephonyManager.getAllCellInfo();
                // List<NeighboringCellInfo> infos = telephonyManager.getNeighboringCellInfo();
                StringBuffer sb = new StringBuffer("总数 : " + infos.size() + "\n");
                for (CellInfo info1 : infos) { // 根据邻区总数进行循环
                    //  sb.append(" LAC : " + info1.getLac()); // 取出当前邻区的LAC
                    //  sb.append(" CID : " + info1.getCid()); // 取出当前邻区的CID
                    sb.append(" CID : " + info1.toString()); // 取出当前邻区的CID
                    //  sb.append(" BSSS : " + (-113 + 2 * info1.getRssi()) + "\n"); // 获取邻区基站信号强度
                }

                Log.i(TAG, " 获取邻区基站信息:" + sb.toString());
                textView2.setText(sb.toString());
            }
        });
        telephonyManager.listen(MyListener, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
       // telephonyManager.listen(celllistener, PhoneStateListener.LISTEN_CELL_LOCATION); // 基站位置的变化
    }



    private class MyPhoneStateListener extends PhoneStateListener
    {
        /* Get the Signal strength from the provider, each tiome there is an update  从得到的信号强度,每个tiome供应商有更新*/
        @Override

        public void onSignalStrengthsChanged(SignalStrength signalStrength)
        {
            super.onSignalStrengthsChanged(signalStrength);
            if (signalStrength.getGsmSignalStrength() != 99) {
                Toast.makeText(getApplicationContext(),
                        "Go to Firstdroid!!! GSM Cinr = " + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113) + "dbM", Toast.LENGTH_SHORT).show();
                System.out.println("****" + String.valueOf(signalStrength.getGsmSignalStrength() * 2 - 113));
            }
        }
    }







1个回答

zqbnqsdsmd
zqbnqsdsmd   2016.11.16 02:27
Csdn user default icon
上传中...
上传图片
插入图片