Determine whether a Wi-Fi connection is available and whether a Wi-Fi connection is available
/**
* Determine whether the current connection mode is a Wi-Fi connection
*
* @ Param context
* @ Return
*/
Private static boolean isWifiConnected (Context context)
{
Return getNetworkState (context, ConnectivityManager. TYPE_WIFI) = State. CONNECTED;
}
/**
* Obtain the connection status.
*
* @ Param context
* @ Param networkType
* @ Return
*/
Private static State getNetworkState (Context context, int networkType)
{
ConnectivityManager cm = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo info = cm. getNetworkInfo (networkType );
Return info = null? Null: info. getState ();
}
The above method can be used to determine a normal Wi-Fi network. But now the Xiaomi system can share the PC network through the Xiaomi assistant. If the above method fails, we can use the following method to determine the network status:
/*
* Determine whether Wifi is available
*
* @ Return true indicates that Wifi is available. If the system returns false, the Wifi function is unavailable.
*/
Public static boolean isWifi ()
{
WifiManager wifiMan = (WifiManager) MaxApplication. getInstance (). getSystemService (Context. WIFI_SERVICE );
WifiInfo wifiInfo = wifiMan. getConnectionInfo ();
If (wifiInfo! = Null)
{
If (WifiInfo. getDetailedStateOf (wifiInfo. getSupplicantState () = DetailedState. OBTAINING_IPADDR
| WifiInfo. getDetailedStateOf (wifiInfo. getSupplicantState () = DetailedState. CONNECTED)
{
Return true;
}
}
Return false;
}