Tools related to android Networks
Tools related to android Networks
Stick it to the past and use it.
Package com. activity;
Import java. util. List;
Import android. content. Context;
Import android. location. LocationManager;
Import android.net. ConnectivityManager;
Import android.net. NetworkInfo;
Import android. telephony. TelephonyManager;
/**
* Network-related tools
*/
Public class NetUtils {
/**
* 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 ){
*
* @ 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;
}
}