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 void Checknetworkinfo () {Connectivitymanager conman = // mobile 3G Data Network State Mobile = Conman.getnetworkinfo (connectivitymanager.type_mobile). GetState (); Txt3g.settext (Mobile.tostring ()); // show 3G network connection status // WiFi State wifi = // show WiFi connection status }
Note:
Depending on the security mechanism of Android, when using Connectivitymanager, you must add <uses-permission android:name= in the Androidmanifest.xml " 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 user if(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 Intent (settings.action_wireless_settings)); // Enter the 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).
Android Programming Get network connection status and call network Configuration interface