Android detects network connection status
When the Android APP needs to connect to the network, it does not connect to the network every time. Therefore, you need to check the network status of the current device in the program so that users can be reminded in a timely manner.
To determine the network status, you must have the following permission code (AndroidManifest. xml ):
Code used to check the current network status:
Public void checkNetworkState (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = mConnectivityManager. getActiveNetworkInfo (); // 1. determine whether there is a network connection boolean networkAvailable = networkInfo. isAvailable (); // 2. obtains the type of the current network connection. int networkType = networkInfo. getType (); if (ConnectivityManager. TYPE_WIFI = networkType) {// currently wifi network} else if (ConnectivityManager. TYPE_MOBILE = networkType) {// currently mobile network }}}