Android Programming Get network connection status and call network Configuration interface

Source: Internet
Author: User

Get Network Connection Status

With the promotion of 3G and WiFi, more and more Android applications need to call network resources and detect the network connection status becomes the necessary function of network application.

The Android platform provides the Connectivitymanager class for detection of network connection status.

The Android Development documentation describes the role of Connectivitymanager in this way:

Class that answers queries on the state of the network connectivity. It also notifies applications when network connectivity changes. Get An instance of the This class by Callingcontext.getsystemservice (Context.connectivity_service).

The primary responsibilities of this class is to:

Monitor Network Connections (Wi-Fi, GPRS, UMTS, etc) Send broadcast intents When network connectivity changes attempt to ' fail over ' to another network when connectivity to a Network is lost

Provide an API, allows applications to query the coarse-grained or fine-grained state of the available networks

The following simple example checknetworkinfo () shows how to programmatically get the current network status of an Android phone

Private voidChecknetworkinfo () {Connectivitymanager conman=(Connectivitymanager) Getsystemservice (context.connectivity_service);//Mobile 3G Data NetworkState Mobile=Conman.getnetworkinfo (connectivitymanager.type_mobile). GetState (); Txt3g.settext (mobile.tostring ());//Show 3G network connection Status//WiFiState WiFi=Conman.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); Txtwifi.settext (wifi.tostring ());//Show WiFi connection status}

Note:

Depending on the security mechanism of Android, when using Connectivitymanager , you must add <uses-in androidmanifest.xml Permission Android:name= "Android.permission.ACCESS_NETWORK_STATE"/> otherwise the system license cannot be obtained.

Running results (off of 3G and WiFi network connection)

Call the Android phone's network Configuration interface

The use of mobile phones on the Android phone QQ friends, should know that when the QQ startup, if there is no effective network connection, QQ will prompt into the mobile phone network configuration interface. How is this achieved? It's very simple, actually.

Private voidChecknetworkinfo () {Connectivitymanager conman=(Connectivitymanager) Getsystemservice (context.connectivity_service);//Mobile 3G Data NetworkState Mobile=Conman.getnetworkinfo (connectivitymanager.type_mobile). GetState (); Txt3g.settext (mobile.tostring ());//WiFiState WiFi=Conman.getnetworkinfo (Connectivitymanager.type_wifi). GetState (); Txtwifi.settext (wifi.tostring ());//if both the 3G and WiFi networks are not connected and are not in the connected state, the network setting interface is configured by the userif(Mobile = = State.connected | | mobile = =state.connecting)return;if(WiFi = = state.connected | | wifi = =state.connecting)return; StartActivity (NewIntent (settings.action_wireless_settings));//Enter the wireless network configuration Interface//startactivity (New Intent (settings.action_wifi_settings));// //access the WiFi network Settings screen in your phone}

Running results (off the status of 3G and WiFi network connection), the program is transferred to the wireless network configuration Interface

StartActivity (new// Enter wireless network Configuration Interface

If you call

StartActivity (new// Direct access to the WiFi Network Settings screen in the phone

Then go directly to the WiFi network Settings screen in your phone

Run the program after a WiFi network connection

We can see that the Wi-Fi status is connected (CONNECTED).

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.