Get Network Connection Status
With the spread of 3G and WiFi, more and more Android applications need to invoke network resources, and detection of network connectivity is a necessary feature of network applications.
The Android platform provides a Connectivitymanager class for detection of network connectivity status.
The Android development document describes Connectivitymanager's role in this way:
Class that answers queries about the state of network connectivity. It also notifies applications when network connectivity changes. Get a instance of this class by calling
Context.getsystemservice (Context.connectivity_service).
The primary responsibilities of this class are 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 this allows applications to query the coarse-grained or fine-grained state of the available
The following simple example checknetworkinfo () shows how to programmatically get the current network status of the Android phone
private void Checknetworkinfo ()
{
Connectivitymanager conman = (Connectivitymanager) getsystemservice
( Context.connectivity_service);
Mobile 3G Data Network state
mobile = Conman.getnetworkinfo (Connectivitymanager.type_mobile). GetState ();
Txt3g.settext (Mobile.tostring ()); Show 3G network connection status
//wifi state
WiFi = Conman.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();
Txtwifi.settext (Wifi.tostring ()); Show WiFi connection Status
}
Note:
According to the Android security mechanism, when using Connectivitymanager, you must add the <uses-permission in the Androidmanifest.xml
Android:name= "Android.permission.ACCESS_NETWORK_STATE"/> cannot obtain permission from the system.
Run result (turn off 3G and WiFi network connection state)
Call the Android phone's network Configuration interface
Use the mobile phone QQ friends, you should know, when the QQ started, if there is no effective network connection, QQ will prompt into the mobile phone network configuration interface. How did this happen?
private void Checknetworkinfo ()
{
Connectivitymanager conman = (Connectivitymanager) getsystemservice
( Context.connectivity_service);
Mobile 3G Data Network state
mobile = Conman.getnetworkinfo (Connectivitymanager.type_mobile). GetState ();
Txt3g.settext (Mobile.tostring ());
WiFi state
WiFi = Conman.getnetworkinfo (Connectivitymanager.type_wifi). GetState ();
Txtwifi.settext (Wifi.tostring ());
If the 3G network and WiFi network are not connected and are not in a connected state, enter the network setting interface by user-configured network connection if
(mobile==state.connected| | mobile==state.connecting) return
;
if (wifi==state.connected| | wifi==state.connecting) return
;
StartActivity (New Intent (settings.action_wireless_settings));//Enter the wireless network Configuration Interface
//startactivity (New Intent ( settings.action_wifi_settings)); Access to the WiFi network Setup interface in your phone