Android-verify that the Network is available

Source: Internet
Author: User

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 "/>"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.