判断有没有 SIM 卡
获取信号强度
判断有没有 SIM 卡获取移动网络的信号,一定要判断是否插了 SIM 卡,没插卡肯定是没法获取的
/**
* 判断是否包含SIM卡
*
* @return 状态
*/
public static boolean hasSimCard(Context context) {
TelephonyManager telMgr = (TelephonyManager)
context.getSystemService(Context.TELEPHONY_SERVICE);
int simState = telMgr.getSimState();
boolean result = true;
switch (simState) {
case TelephonyManager.SIM_STATE_ABSENT:
case TelephonyManager.SIM_STATE_UNKNOWN:
result = false; // 没有SIM卡
break;
}
return result;
}
获取信号强度如果插了 SIM 卡,就可以通过下边方法来获取移动网络的信号强度(单位 dBM),使用的是 TelephonyManager 来监听
private void getMobileNetworkSignal() {
if (!PhoneUtils.hasSimCard(mcontext)) {
logger.info("getMobileNetworkSignal: no sim card");
return;
}
TelephonyManager mTelephonyManager = (TelephonyManager) BaseApplication.getAppContext().getSystemService(Context.TELEPHONY_SERVICE);
if (mTelephonyManager != null) {
mTelephonyManager.listen(new PhoneStateListener() {
@Override
public void onSignalStrengthsChanged(SignalStrength signalStrength) {
super.onSignalStrengthsChanged(signalStrength);
int asu = signalStrength.getGsmSignalStrength();
int lastSignal = -113 + 2 * asu;
if (lastSignal > 0) {
mobileNetworkSignal = lastSignal + "dBm";
}
logger.info("Current mobileNetworkSignal:" + lastSignal + " dBm");
}
}, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS);
}
}
也可以添加网络类型的判断,来获取是何种网络的信号强度
int netWorkType = getNetWorkType(mContext);
switch (netWorkType) {
case NETWORKTYPE_WIFI:
mTextView.setText("当前网络为wifi,信号强度为:" + gsmSignalStrength);
break;
case NETWORKTYPE_2G:
mTextView.setText("当前网络为2G移动网络,信号强度为:" + gsmSignalStrength);
break;
case NETWORKTYPE_4G:
mTextView.setText("当前网络为4G移动网络,信号强度为:" + gsmSignalStrength);
break;
case NETWORKTYPE_NONE:
mTextView.setText("当前没有网络,信号强度为:" + gsmSignalStrength);
break;
case -1:
mTextView.setText("当前网络错误,信号强度为:" + gsmSignalStrength);
break;
}
用到的获取网络类型的方法:
public static int getNetWorkType(Context context) {
int mNetWorkType = -1;
ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = manager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected()) {
String type = networkInfo.getTypeName();
if (type.equalsIgnoreCase("WIFI")) {
mNetWorkType = NETWORKTYPE_WIFI;
} else if (type.equalsIgnoreCase("MOBILE")) {
return isFastMobileNetwork(context) ? NETWORKTYPE_4G : NETWORKTYPE_2G;
}
} else {
mNetWorkType = NETWORKTYPE_NONE;//没有网络
}
return mNetWorkType;
}
/**
* 判断网络类型
*/
private static boolean isFastMobileNetwork(Context context) {
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
if (telephonyManager.getNetworkType() == TelephonyManager.NETWORK_TYPE_LTE) {
return true;
}
return false;
}
最后添加必要权限即可
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
以上就是Android获取移动网络信号强度的方法的详细内容,更多关于Android获取网络信号强度的资料请关注软件开发网其它相关文章!