SoFunction
Updated on 2025-03-11

Android to obtain and monitor the network status of mobile phones

1. Get the current mobile phone networking method or mobile phone 4G data

private boolean getNetworkType(){
        ConnectivityManager mConnectivity = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
        //Check the network link        NetworkInfo info = ();
        int netType = ().getType();
        if (netType == ConnectivityManager.TYPE_WIFI) {  //WIFI
            (TAG,"CurrentlyWIFIconnect isConnected = "+());
            return ();
        } else if (netType == ConnectivityManager.TYPE_MOBILE) {   //MOBILE
            (TAG,"Currently手机网络connect isConnected = "+());
            return ();
        } else {
            (TAG,"当前没有网络connect isConnected = "+());
            return false;
        }
    }

2. Monitor the changes in mobile phone network

private class MyBroadcastReceiver extends BroadcastReceiver{
        @Override
        public void onReceive(Context context, Intent intent) {
            if (().equals(ConnectivityManager.CONNECTIVITY_ACTION)) {// Listen to network connection                //Get the NetworkInfo object with network status                NetworkInfo info = (ConnectivityManager.EXTRA_NETWORK_INFO);
                if (info != null) {
                    //If the current network connection is successful and the network connection is available                    if ( == () && ()) {
                        if (() == ConnectivityManager.TYPE_WIFI){
                            (TAG,"Connect on WiFi");
                        }else if (() == ConnectivityManager.TYPE_MOBILE){
                            (TAG,"Connect on mobile network data");
                            ().gameEvent("Network_4G_reminder");
                        }
                    } else {
                        (TAG,"Network Disconnected");
                    }
                }
            }
        }
    }

Note: Network status permissions are required

<uses-permission android:name=".ACCESS_NETWORK_STATE" />
<uses-permission android:name=".ACCESS_WIFI_STATE" />

Summarize

This is the article about obtaining and monitoring the network status of mobile phones. For more related content on Android network status, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!