1. Determine whether a network is available
Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. main); </P> <p> connectivitymanager mconnectivitymanager = (connectivitymanager) This. getsystemservice (context. connectivity_service); <br/> networkinfo mnetworkinfo = mconnectivitymanager. getactivenetworkinfo (); <br/> If (mnetworkinfo. isavailable () <br/> system. out. println ("***** there is available net work"); <br/> else <br/> system. out. println ("***** there is no available net work"); <br/> // obtain whether the specified network type is available, type_movile and type_wifi <br/> mnetworkinfo = mconnectivitymanager. getnetworkinfo (connectivitymanager. type_mobile); <br/> system. out. println ("**** net work type =" + mnetworkinfo. getType (); </P> <p> system. out. println ("*********** extra info =" + mnetworkinfo. getextrainfo (); </P> <p>}
2. Determine whether WiFi is on
Public static Boolean checkwifi (activity activitiy) {<br/> wifimanager mwifimanager = (wifimanager) activitiy <br/>. getsystemservice (context. wifi_service); <br/> wifiinfo = mwifimanager. getconnectioninfo (); <br/> int IPaddress = wifiinfo = NULL? 0: wifiinfo. getipaddress (); <br/> If (mwifimanager. iswifienabled () & IPaddress! = 0) {<br/> system. out. println ("*** WiFi is on"); <br/> return true; <br/>}else {<br/> system. out. println ("**** WiFi is off"); <br/> return false; <br/>}< br/>}