Method for Obtaining Signal Strength of Mobile Network by Android

  • 2021-12-12 09:39:39
  • OfStack

The directory judges whether there is an SIM card Obtain signal strength

Judge whether there is an SIM card

To obtain the signal of mobile network, 1 must judge whether SIM card is inserted, and it is definitely impossible to obtain it without inserting the card


    /**
     *  Determine whether to include SIM Card 
     *
     * @return  Status 
     */
    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;
    }

Obtain signal strength

If the SIM card is inserted, the signal strength of the mobile network (unit dBM) can be obtained by the following method, and TelephonyManager is used to monitor


    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);
        }
    }

You can also add the judgment of network type to obtain the signal strength of which network


int netWorkType = getNetWorkType(mContext);
switch (netWorkType) {
    case NETWORKTYPE_WIFI:
        mTextView.setText(" The current network is wifi, The signal strength is: " + gsmSignalStrength);
        break;
    case NETWORKTYPE_2G:
        mTextView.setText(" The current network is 2G Mobile network , The signal strength is: " + gsmSignalStrength);
        break;
    case NETWORKTYPE_4G:
        mTextView.setText(" The current network is 4G Mobile network , The signal strength is: " + gsmSignalStrength);
        break;
    case NETWORKTYPE_NONE:
        mTextView.setText(" There is currently no network , The signal strength is: " + gsmSignalStrength);
        break;
    case -1:
        mTextView.setText(" Current network error , The signal strength is: " + gsmSignalStrength);
        break;
}

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")) {
            return isFastMobileNetwork(context) ? NETWORKTYPE_4G : NETWORKTYPE_2G;
        }
    } else {
        mNetWorkType = NETWORKTYPE_NONE;// There's no internet access 
    }
    return mNetWorkType;
}

/**
 *  Determine 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;
}

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"/>

The above is the Android access to mobile network signal strength of the detailed content, more information about Android access to network signal strength, please pay attention to other related articles on this site!


Related articles: