Network state judgment of android (wifi and mobile network data)

Keywords: network Android Mobile

Sometimes we need to monitor the network status changes to remind users of the network status in time. Therefore, we need to monitor more network changes. We don't need to talk about much nonsense. We have listed all network situations in the code. If there is a need, we can prompt according to the actual situation.
1. First of all, it is necessary to monitor the broadcast. The change of network status will trigger the broadcast:

//Static registration
<receiver
    android:name=".receiver.StartServiceReceiver"
    android:exported="true">
    <intent-filter>
        <action android:name="android.net.wifi.WIFI_STATE_CHANGED" />//Monitor wifi on and off
        <action android:name="android.net.wifi.STATE_CHANGE" />//Monitor the connection status of wifi
    </intent-filter>
</receiver>

//Dynamic registration, this broadcast can only be received by dynamic registration
IntentFilter filter = new IntentFilter();
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);//Network connection (including wifi and mobile network)

The above is broadcast registration. There are two kinds of broadcast for wifi monitoring, both of which support static registration, but only dynamic registration for the whole network monitoring (wifi + mobile network data).

2. Receive and analyze the broadcast:

public class StartServiceReceiver extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // Monitor whether wifi is on or off, regardless of wifi connection
        if (WifiManager.WIFI_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            int wifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, 0);
            if (wifiState == WifiManager.WIFI_STATE_DISABLED) {//wifi off
                MlogUtil.d("netstatus", "wifi Closed");
            } else if (wifiState == WifiManager.WIFI_STATE_ENABLED) {//wifi on
                MlogUtil.d("netstatus", "wifi Enabled");
            } else if (wifiState == WifiManager.WIFI_STATE_ENABLING) {//wifi on
                MlogUtil.d("netstatus", "wifi Opening");
            } else if (wifiState == WifiManager.WIFI_STATE_DISABLING) {//wifi is shutting down
                MlogUtil.d("netstatus", "wifi Closing");
            }
        }
        // Monitor the connection status of wifi, i.e. whether an effective wireless route is connected
        if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(intent.getAction())) {
            Parcelable parcelableExtra = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
            if (parcelableExtra != null) {
                MlogUtil.d("netstatus", "wifi parcelableExtra Not empty");
                NetworkInfo networkInfo = (NetworkInfo) parcelableExtra;
                if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {//Network connected
                    MlogUtil.d("netstatus", "wifi Network connected");
                    if (networkInfo.isAvailable()) {//And the network is available
                        MlogUtil.d("netstatus", "wifi Network connected and available");
                    } else {//And the network is not available
                        MlogUtil.d("netstatus", "wifi Connected to network, but not available");
                    }
                } else {//Network not connected
                    MlogUtil.d("netstatus", "wifi Network not connected");
                }
            } else {
                MlogUtil.d("netstatus", "wifi parcelableExtra Empty");
            }
        }
        // Monitor network connection, total network judgment, including wifi and mobile network monitoring
        if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
            NetworkInfo networkInfo = intent.getParcelableExtra(ConnectivityManager.EXTRA_NETWORK_INFO);
            //Judgment of connected network type: wifi or mobile network
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                MlogUtil.d("netstatus", "The total network connection is wifi network");
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                MlogUtil.d("netstatus", "The total network is connected to the mobile network");
            }
            //Specific connection status judgment
            checkNetworkStatus(networkInfo);
        }
    }

    private void checkNetworkStatus(NetworkInfo networkInfo) {
        if (networkInfo != null) {
            MlogUtil.d("netstatus", "global network info Not empty");
            if (networkInfo.getState() == NetworkInfo.State.CONNECTED) {//Network connected
                MlogUtil.d("netstatus", "Total network connected");
                if (networkInfo.isAvailable()) {//And the network is available
                    MlogUtil.d("netstatus", "Total network connected and available");
                } else {//And the network is not available
                    MlogUtil.d("netstatus", "Total network connected but not available");
                }
            } else if (networkInfo.getState() == NetworkInfo.State.DISCONNECTED) {//Network not connected
                MlogUtil.d("netstatus", "Total network not connected to network");
            }
        } else {
            MlogUtil.d("netstatus", "global network info Empty");
        }
    }
}

As mentioned above, log printing distinguishes between total network and wifi network judgment. Because the first two are dedicated to wifi network monitoring, and the third one is dedicated to the whole network monitoring, in order to distinguish, the whole network monitoring is called non total network.
So if you want to judge the network, you can judge in the last general network. All kinds of situations have been annotated clearly, and you can remind yourself according to your own situation.

Posted by hermand on Sat, 02 May 2020 04:24:55 -0700