Packagenet.nyist.netState; ImportAndroid.content.Context; ImportAndroid.net.ConnectivityManager; ImportAndroid.net.NetworkInfo; /**@author* This is a tool class to judge the current network status **/ Public classNetstate {/** * * @returnwhether there is an active network connection*/ Public Final Booleanhasnetworkconnection (Context context) {//Get connection Activity manager FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); //Get link Network information FinalNetworkinfo networkinfo=Connectivitymanager.getactivenetworkinfo (); return(networkinfo!=NULL&&networkinfo.isavailable ()); } /** * @returnReturns a Boolean, whether it is a WiFi network **/ Public Final Booleanhaswificonnection (Context context) {FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); FinalNetworkinfo networkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); //whether there is a network and is already connected return(networkinfo!=NULL&&networkinfo.isconnectedorconnecting ()); } /** * @returnreturns a Boolean that determines whether the network is available, whether it is a mobile network **/ Public Final Booleanhasgprsconnection (Context context) {//Get active Connection Manager FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); FinalNetworkinfo networkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_mobile); return(networkinfo!=NULL&&networkinfo.isavailable ()); } /** * @returndetermine if the network is available and return the network type, Connectivitymanager.type_wifi,connectivitymanager.type_mobile, unavailable return-1*/ Public Static Final intGetnetworkconnectiontype (Context context) {FinalConnectivitymanager connectivitymanager=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); FinalNetworkinfo wifinetworkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); FinalNetworkinfo mobilenetworkinfo=Connectivitymanager.getnetworkinfo (Connectivitymanager.type_mobile); if(wifinetworkinfo!=NULL&&wifinetworkinfo.isavailable ()) { returnConnectivitymanager.type_wifi; } Else if(mobilenetworkinfo!=NULL&&mobilenetworkinfo.isavailable ()) { returnConnectivitymanager.type_mobile; } Else { return-1; } } }
Android Current network connection type judging