Network: Android network judgment (wifi, 3G, and others)
Public class NetworkProber {
/**
* Whether the Network is available
*
* @ Param activity
* @ Return
*/
Public static boolean isNetworkAvailable (Context context ){
ConnectivityManager connectivity = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
If (connectivity = null ){
} Else {
NetworkInfo [] info = connectivity. getAllNetworkInfo ();
If (info! = Null ){
For (int I = 0; I <info. length; I ++ ){
If (info [I]. getState () = NetworkInfo. State. CONNECTED ){
Return true;
}
}
}
}
Return false;
}
/**
* Whether or not Gps is enabled
*
* @ Param context
* @ Return
*/
Public static boolean isGpsEnabled (Context context ){
LocationManager locationManager = (LocationManager) context
. GetSystemService (Context. LOCATION_SERVICE ));
List AccessibleProviders = locationManager. getProviders (true );
Return accessibleProviders! = Null & accessibleProviders. size ()> 0;
}
/**
* Whether wifi is enabled
*/
Public static boolean isWifiEnabled (Context context ){
ConnectivityManager mgrConn = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
TelephonyManager mgrTel = (TelephonyManager) context
. GetSystemService (Context. TELEPHONY_SERVICE );
Return (mgrConn. getActiveNetworkInfo ()! = Null & mgrConn
. GetActiveNetworkInfo (). getState () = NetworkInfo. State. CONNECTED) | mgrTel
. GetNetworkType () = TelephonyManager. NETWORK_TYPE_UMTS );
}
/**
* Determine whether the current network is a Wi-Fi network
* If (activeNetInfo. getType () = ConnectivityManager. TYPE_MOBILE) {// judge the 3G network
*
* @ Param context
* @ Return boolean
*/
Public static boolean isWifi (Context context ){
ConnectivityManager connectivityManager = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager. getActiveNetworkInfo ();
If (activeNetInfo! = Null
& ActiveNetInfo. getType () = ConnectivityManager. TYPE_WIFI ){
Return true;
}
Return false;
}
/**
* Determine whether the current network is a 3G network
*
* @ Param context
* @ Return boolean
*/
Public static boolean is3G (Context context ){
ConnectivityManager connectivityManager = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo activeNetInfo = connectivityManager. getActiveNetworkInfo ();
If (activeNetInfo! = Null
& ActiveNetInfo. getType () = ConnectivityManager. TYPE_MOBILE ){
Return true;
}
Return false;
}
}
There are two other ways to determine whether the Network is available:
Public static boolean isNetworkAvailable_00 (Context context ){
ConnectivityManager cm = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE ));
If (cm! = Null ){
NetworkInfo info = cm. getActiveNetworkInfo ();
If (info! = Null & info. isConnectedOrConnecting ()){
Return true;
}
}
Return false;
}
Public static boolean isNetworkAvailable_01 (Context context ){
ConnectivityManager cm = (ConnectivityManager) context
. GetSystemService (Context. CONNECTIVITY_SERVICE );
NetworkInfo network = cm. getActiveNetworkInfo ();
If (network! = Null ){
Return network. isAvailable ();
}
Return false;
}
More rigorous writing:
Public static boolean checkNet (Context context ){
Try {
ConnectivityManager connectivity = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE );
If (connectivity! = Null ){
NetworkInfo info = connectivity. getActiveNetworkInfo ();
If (info! = Null & info. isConnected ()){
If (info. getState () = NetworkInfo. State. CONNECTED ){
Return true;
}
}
}
} Catch (Exception e ){
Return false;
}
Return false;
}