There is nothing to say!
Package mark. zhang; import Java. util. list; import android. content. context; import android. location. locationmanager; import android.net. connectivitymanager; import android.net. networkinfo; import android. telephony. telephonymanager; public class networkprober {/*** network availability ** @ Param activity * @ return */public static Boolean isnetworkavailable (context) {connectivitymanager connectivity = (connectiv Itymanager) 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;}/*** determines if GPS is enabled ** @ Param context * @ return */public static Boolean isgpsenabled (context) {locationmanager = (locationmanager) context. getsystemservice (context. location_service); List <string> accessibleproviders = locationmanager. getproviders (True); Return accessibleproviders! = NULL & accessibleproviders. size ()> 0;}/*** whether WiFi is enabled */public static Boolean iswifienabled (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);}/*** determines whether the current network is a Wi-Fi network * If (activenetinfo. getType () = connectivitymanager. type_mobile) {// determine the 3G network ** @ Param context * @ return Boolean */public static Boolean iswifi (context) {connectivitymanager = (connectivitymanager) Conte XT. 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) {connectivitymanager = (connectivitymanager) context. getsystemservice (context. connectivity_service); networkinfo activenetinfo = connectivitymanager. getactivenetworkinfo (); If (activene Tinfo! = 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;}