First of all, to get the current Android device is connected, then Android itself provides us with a service
Private Connectivitymanager Connectivitymanager; // used to determine if there is a network = (Connectivitymanager) getactivity (). Getsystemservice (Context.connectivity_service); // Get the connection service for the current network // get the active network connection information
First we have to get the Android Network service, need to add a permission to the configuration file
<!--- <android:name = " Android.permission.ACCESS_NETWORK_STATE "/>
Then we get the system service through Context.getsystemservice (Context.connectivity_service)
Then we use Android to provide us with this networkinfo class to go to the system return to our network service information
if null) { // There is currently no active network connection (indicates that the user has turned off the data traffic service and has not turned on WiFi and other data Services) Toast.maketext (getactivity (), " Check that the network connection is turned on, Toast.length_short). Show ( ); Else { // There is currently an active network connection }
Of course, we can also use this code to determine whether our network connection is available
// to return is valid, if True indicates that the current Android phone is already networked, possibly WiFi or GPRS, HSDPA, and so on. Specifically, the Getactivenetworkinfo () method of the Connectivitymanager class can be used to determine the detailed access method Connectivitymanager.getactivenetworkinfo (). IsAvailable ();
2015-10-23
Android Determines if the current Android device is networked