Detects if the current network is available, as follows:
/** * Detect when the network (WLAN, 3g/2g) status * @param context * @return True indicates that the network is available */ Public Static Boolean isnetworkavailable(Context context) {Connectivitymanager connectivity = (Connectivitymanager) context. Getsystemservice (Context.connectivity_se Rvice);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;}
To add network-related permissions in the Androidmanifest.xml file:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.INTERNET"/>
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Android detects network status to determine if the current network is available