Check whether there is a SIM card
Get the signal of mobile network, must judge whether insert SIM card, do not insert card is certainly can’t get
/** * Check whether SIM card ** is included@returnState * /
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; // No SIM card
break;
}
return result;
}
Copy the code
Acquisition of signal strength
If a SIM card is inserted, the following method can be used to obtain the signal strength of the mobile network (in dBM), using TelephonyManager to listen
private void getMobileNetworkSignal(a) {
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("The Current mobileNetworkSignal:" + lastSignal + " dBm"); } }, PhoneStateListener.LISTEN_SIGNAL_STRENGTHS); }}Copy the code
You can also add network type judgments to get the signal strength of which network
int netWorkType = getNetWorkType(mContext);
switch (netWorkType) {
case NETWORKTYPE_WIFI:
mTextView.setText("The current network is wifi, and the signal strength is:" + gsmSignalStrength);
break;
case NETWORKTYPE_2G:
mTextView.setText("The current network is a 2G mobile network and the signal strength is:" + gsmSignalStrength);
break;
case NETWORKTYPE_4G:
mTextView.setText("The current network is a 4G mobile network and the signal strength is:" + gsmSignalStrength);
break;
case NETWORKTYPE_NONE:
mTextView.setText("Currently no network, signal strength is:" + gsmSignalStrength);
break;
case -1:
mTextView.setText("Current network error, signal strength:" + gsmSignalStrength);
break;
}
Copy the code
The method used to get the network type:
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")) {
returnisFastMobileNetwork(context) ? NETWORKTYPE_4G : NETWORKTYPE_2G; }}else {
mNetWorkType = NETWORKTYPE_NONE;// No network
}
return mNetWorkType;
}
/** * Check the network type */
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;
}
Copy the code
Finally, add the necessary permissions
<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"/>
Copy the code