问题描述
- 在 android 中如何获得网络强度
-
我想在手机上面显示网络的网络信号强度。
现在我可以通过下面的代码检查出是否连接到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 "); }
解决方案
好像不行吧,不知4.0以后的版本有没有提供相应的方法
解决方案二:
试一下:
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
}
}
时间: 2024-09-14 13:53:44