2 clcurie 11 clcurie_11 于 2013.03.28 11:10 提问

如何在应用程序中获取网络强度?

我想在手机设备中显示网络的网络强度。
现在我可以检查到 wifi 的连接。但是我需要知道网络信号强度,我要在下面的代码基础上再添加什么代码?

ConnectivityManager connectivityManager = (ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetInfo = connectivityManager
                .getActiveNetworkInfo();
        NetworkInfo mobNetInfo = connectivityManager
                .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
           TextView netStatus = (TextView) findViewById(R.id.netStatus);
        if (activeNetInfo != null) {
            netStatus.setText(" Connection Status - Connected "+activeNetInfo.getTypeName());
        } else if (mobNetInfo != null) {
            netStatus.setText(" Connection Status - Connected "+mobNetInfo.getTypeName());
        } else {
            netStatus.setText(" Connection Status - Not Connected ");
        }

2个回答

hxn_217
hxn_217   2013.04.02 10:37
已采纳

使用下面的代码:

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo Info = cm.getActiveNetworkInfo();
    if (Info == null || !Info.isConnectedOrConnecting()) {
        Log.i(TAG, "No connection");
    } else {
        int netType = Info.getType();
        int netSubtype = Info.getSubtype();

        if (netType == ConnectivityManager.TYPE_WIFI) {
            Log.i(TAG, "Wifi connection");
            WifiManager wifiManager = (WifiManager) getApplication().getSystemService(Context.WIFI_SERVICE);
            List<ScanResult> scanResult = wifiManager.getScanResults();
            for (int i = 0; i < scanResult.size(); i++) {
                Log.d("scanResult", "Speed of wifi"+scanResult.get(i).level);//The db level of signal 
            }


            // Need to get wifi strength
        } else if (netType == ConnectivityManager.TYPE_MOBILE) {
            Log.i(TAG, "GPRS/3G connection");
            // Need to get differentiate between 3G/GPRS
        }
    }
baimy1985
baimy1985   2013.03.28 16:06
wifi的信号强度可以这样获取
mWifiManager =(WifiManager)mContext.getSystemService(Context.WIFI_SERVICE);
mWifiManager.getConnectionInfo().getRssi()
对应的就是下面两个方法
 public WifiInfo getConnectionInfo() {
        try {
            return mService.getConnectionInfo();
        } catch (RemoteException e) {
            return null;
        }
  }
    public int getRssi() {
        return mRssi;
    }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!