Connectivitymanager mainly manages network connection-related operations. The networkinfo class includes a detailed description of the two network modes of WiFi and mobile, through its getstate () the State object obtained by the method indicates whether the connection is successful or not.
CONNECTED
Detailed state |
Coarse-grained state |
IDLE |
DISCONNECTED |
SCANNING |
CONNECTING |
CONNECTING |
CONNECTING |
AUTHENTICATING |
CONNECTING |
CONNECTED |
DISCONNECTING |
DISCONNECTING |
DISCONNECTED |
DISCONNECTED |
UNAVAILABLE |
DISCONNECTED |
FAILED |
DISCONNECTED |
State state = connectivitymanager. getnetworkinfo (connectivitymanager. type_mobile). getstate ();
If (State. Connected = State ){
Log. I ("tag", "GPRS network connected ");
}
State = connectivitymanager. getnetworkinfo (connectivitymanager. type_wifi). getstate ();
If (State. Connected = State ){
Log. I ("tag", "Wi-Fi network connected ");
}
// Determine whether the Network is available </P> <p> Public static Boolean isnetworkavaiable (context) {<br/> connectivitymanager = (connectivitymanager) context. getsystemservice (connectivity_service); <br/> networkinfo info = connectivitymanager. getactivenetworkinfo (); // get the networkinfo object that represents the networking status <br/> return (info! = NULL & info. isconnected (); <br/>}</P> <p> // determines whether WiFi is available </P> <p> Public static Boolean iswifiactive (context) {</P> <p> connectivitymanager = (connectivitymanager) context. getsystemservice (connectivity_service); <br/> networkinfo [] info = connectivitymanager. getallnetworkinfo (); <br/> If (info! = NULL) {<br/> for (INT I = 0; I <info. length; I ++) {<br/> // <br/> If (info [I]. gettypename (). equals (connectivitymanager. type_wifi) & info [I]. isconnected () <br/> return true; <br/>}< br/> return false; <br/>}< br/>
Connectivitymanager. type_wifi indicates a Wi-Fi connection.
Connectivitymanager. type_mobile indicates a 3G connection.
Network Settings intent:
New intent (Android. provider. settings. action_wireless_settings)
// Jump to the wireless network settings page
New intent (Android. provider. settings. action_wifi_settings) // jump to the Wireless WiFi network settings page
Finally, do not forget to add the permission to the mainifest file:
"<Uses-Permission Android: Name =" android. Permission. access_network_state "/>"