first of all, to get the current Android device is connected, then Android itself provides us with a service
Privateconnectivitymanager connectivitymanager;//is used to determine if there is a network Connectivitymanager = (Connectivitymanager) getActivity ( ). Getsystemservice (Context.connectivity_service);//Gets the connection service for the current network Networkinfo info =connectivitymanager.getact Ivenetworkinfo (); Get the active network connection information
first we have to get the Android Network Service, need to add a permission to the configuration file
<!--get information about whether a user's phone is networked--><uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
and then we passed Context.getsystemservice(context.connectivity_service) Get the system service
then we use Android to provide us with this Networkinfo class to go to the system return to our network service information
if (info = = null) {//does not currently have an activated network connection (indicating that the user has turned off the data traffic service, or turned on wifi and other data Services) Toast.maketext (Getactivity (), "Check network Whether the connection is open, 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 ();
of course, after the development APP It is also necessary to conduct a full range of tests: www.ineice.com
Android determines whether the current Android device is connected