The user's current network is available: WiFi, 2g/3g Network, users open and not open the network, and whether can be used is not the same thing. Can use refers to: users open the network and can connect to the Internet for Internet access.
To detect if the current network is available, the code is as follows:
/*** Detect when the network (WLAN, 3g/2g) status *@paramContext Context *@returnTrue indicates that the network is available*/ Public Static Booleanisnetworkavailable (Context context) {Connectivitymanager Connectivity=(Connectivitymanager) context. Getsystemservice (Context.connectivity_service); if(Connectivity! =NULL) {Networkinfo info=Connectivity.getactivenetworkinfo (); if(Info! =NULL&&info.isconnected ()) { //the current network is connected if(info.getstate () = =NetworkInfo.State.CONNECTED) {//the network currently connected is available return true; } } } return false; }
Permissions added in the Androidmanifest.xml file:
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> <uses-permission Android : Name= "Android.permission.INTERNET"/>
http://blog.csdn.net/android_ls/article/details/8605991
Android detects network status and determines if the current network is available