PackageCom.fredric.util;ImportAndroid.content.Context;ImportAndroid.net.ConnectivityManager;ImportAndroid.net.NetworkInfo;Importandroid.net.NetworkInfo.State;/*-* Network Tool class*/ Public classNetUtils {//determine if the network is connected Public Static Booleanisconnected (Context context) {Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); //five states of WLAN//1, show that the connection has been saved, but the title bar does not, that is, no physical connection on the output as: Not Connect, available//2, show the connection is saved, the title bar also has a connected icon, the output is: Connect, available//3, select Do not save after output as: Not Connect, available//4, select connection, output when acquiring IP address: Not connect, not available//5. After connection, the output is: Connect, available if(NULL!=connectivity) {Networkinfo Info=Connectivity.getactivenetworkinfo (); if(NULL! = Info &&info.isconnected ()) { return true; } } return false; } //determine if the WiFi connection Public Static BooleanIswificonnect (Context context) {Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(NULL!=connectivity) { State State=connectivity. Getnetworkinfo (Connectivitymanager.type_wifi). GetState (); if(state = =state.connected) { return true; } } return false; } //determine if GPRS is connected Public Static BooleanIsgprsconnect (Context context) {Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(NULL!=connectivity) { State State=connectivity. Getnetworkinfo (Connectivitymanager.type_mobile). GetState (); if(state = =state.connected) { return true; } } return false; }}
Public Code Reference (Connectivitymanager)