Http://www.mobiletuts.me
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 calling Context.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
PrivatevoidChecknetworkinfo ()
{
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:
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.
PrivatevoidChecknetworkinfo ()
{
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 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 Intent (settings.action_wifi_settings)); // direct access to the WiFi network Settings screen in your 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).
My Contact information:
Http://www.mobiletuts.me
Or
mobiletuts.me#gmail.com (replace #with @)
Android Programming Get network Connection status (3G/WIFI) and call network Configuration interface