Package Mark.zeng; 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 classNetworkprober {/** * Network Availability * * @param activity * @return*/ Public StaticBoolean isnetworkavailable (context context) {Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(Connectivity = =NULL) { } Else{networkinfo[] info=Connectivity.getallnetworkinfo (); if(Info! =NULL) { for(inti =0; i < info.length; i++) { if(info[i].getstate () = =NetworkInfo.State.CONNECTED) {return true; } } } } return false; } /** * GPS Open * * @param context * @return*/ Public StaticBoolean isgpsenabled (context context) {Locationmanager Locationmanager=((Locationmanager) context. Getsystemservice (Context.location_service)); List<String> accessibleproviders = Locationmanager.getproviders (true); returnAccessibleproviders! =NULL&& accessibleproviders.size () >0; } /** * WiFi is open*/ Public StaticBoolean 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 if the current network is a WiFi network * if (Activenetinfo.gettype () ==connectivitymanager.type_mobile) {//judgment 3G Net * * @param context * @return Boolean*/ Public StaticBoolean 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 if the current network is a 3G network * * @param context * @return Boolean*/ Public StaticBoolean 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 additional ways to determine if a network is available: Public StaticBoolean 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 StaticBoolean isnetworkavailable_01 (context context) {Connectivitymanager cm=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); Networkinfo Network=Cm.getactivenetworkinfo (); if(Network! =NULL) { returnnetwork.isavailable (); } return false; A more rigorous notation: Public StaticBoolean 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; }
Network:android Network Judgment (WiFi, 3G and others)