/** * Detect if the network is available 1 * @param Act * @return */public boolean isnetconnected (Activity Act) { Connectivitymanager manager = (connectivitymanager) Act. Getapplicationcontext (). Getsystemservi CE (context.connectivity_service); if (manager = = null) {return false; } networkinfo Networkinfo = Manager.getactivenetworkinfo (); if (Networkinfo = = NULL | |!networkinfo.isavailable ()) {return false; } return true; }/** * Detects if the network is available 2 */private boolean isnetconnected () {Pppoemanager Pppoemanager = pppoemanager.getinstance ( ); Ethernetmanager Ethernetmanager = Ethernetmanager.getinstance (); Connectivitymanager Connectivitymanager = (connectivitymanager) getsystemservice (Connectivity_service); Networkinfo networkinfo = Connectivitymanager.getnetworkinfo (conneCtivitymanager.type_wifi); if (networkinfo.isconnected () | | ethernetmanager.isnetworkconnected () | | pppoemanager.getpppoestatus (). Equa LS (pppoemanager.pppoe_state_connect)) {return true; } else {return false; } }
Increase network permissions
<uses-permission android:name=
" Android.permission.ACCESS_NETWORK_STATE "
/>
Check if the network is available