【Android】Detailed Android network operation

Directory Structure:

contents structure [+]

To obtain network information, you need to add the corresponding permissions to the AndroidManifest.xml file.

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

1. Judge the network

1.1 Determine if there is a network connection

public boolean isNetworkConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null) {
return mNetworkInfo.isAvailable();
}
}
return false;
}

1.2 Determine whether the WIFI network is available

public boolean isWifiConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWiFiNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (mWiFiNetworkInfo != null) {
return mWiFiNetworkInfo.isAvailable();
}
}
return false;
}

It should be noted here that this method only judges whether WIFI is available in the current environment, not whether WIFI is already connected.

1.3 Determine whether the MOBILE network is available

public boolean isMobileConnected(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mMobileNetworkInfo = mConnectivityManager
.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
if (mMobileNetworkInfo! = null ) {
 return mMobileNetworkInfo.isAvailable ();
}
}
return false;
}

1.4 Get the type information of the current network connection

public static int getConnectedType(Context context) {
if (context != null) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
return mNetworkInfo.getType();
}
}
return -1;
}

The return value of getType() can be TYPE_MOBILE, TYPE_WIFI, TYPE_WIMAX, TYPE_ETHERNET, TYPE_BLUETOOTH, and other types in ConnectivityManager.

2. Monitor the network

 Define the broadcast receiver:

    BroadcastReceiver connectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager mConnectivityManager = (ConnectivityManager) context
                    .getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo mNetworkInfo = mConnectivityManager
                    .getActiveNetworkInfo();
            int type = -1;
            if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {
                type = mNetworkInfo.getType();
            }
            if (type == ConnectivityManager.TYPE_WIFI) {
                Toast.makeText(MainActivity.this, "wifi", Toast.LENGTH_SHORT)
                        .show();
            } else if (type == ConnectivityManager.TYPE_MOBILE) {
                Toast.makeText(MainActivity.this , "Network Available" , Toast.LENGTH_SHORT )
                .show();
            }else if(type==-1){
                Toast.makeText(MainActivity.this , "Network unavailable" , Toast.LENGTH_SHORT )
                .show();
            }else{
                Toast.makeText(MainActivity.this , "Unknown Network" , Toast.LENGTH_SHORT )
                .show();
            }
        }
    };

Register at the appropriate location:

        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(connectionReceiver, intentFilter);

Unregister at the appropriate location:

unregisterReceiver(connectionReceiver);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325150249&siteId=291194637