Source: http://www.jb51.net/article/32920.htm
Obtaining network information requires that the appropriate permissions be added to the Androidmanifest.xml file.
<uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/>
1 Public Booleanisnetworkconnected (Context context) {2 if(Context! =NULL) { 3 4Connectivitymanager Mconnectivitymanager =(connectivitymanager) Context5 . Getsystemservice (context.connectivity_service);6 7Networkinfo Mnetworkinfo =mconnectivitymanager.getactivenetworkinfo ();8 9 if(Mnetworkinfo! =NULL) { Ten One returnmnetworkinfo.isavailable (); A - } - the } - - return false; -}One, determine if there is an Internet connection
1 Public Booleaniswificonnected (Context context) {2 if(Context! =NULL) { 3 4Connectivitymanager Mconnectivitymanager =(connectivitymanager) Context5 . Getsystemservice (context.connectivity_service);6 7Networkinfo Mwifinetworkinfo =Mconnectivitymanager8 . Getnetworkinfo (Connectivitymanager.type_wifi);9 Ten if(Mwifinetworkinfo! =NULL) { One A returnmwifinetworkinfo.isavailable (); - - } the - } - - return false; +}Two. Determine if the WiFi network is available
1 Public Booleanismobileconnected (Context context) {2 3 if(Context! =NULL) { 4 5Connectivitymanager Mconnectivitymanager =(connectivitymanager) Context6 . Getsystemservice (context.connectivity_service);7 8Networkinfo Mmobilenetworkinfo =Mconnectivitymanager9 . Getnetworkinfo (connectivitymanager.type_mobile);Ten One if(Mmobilenetworkinfo! =NULL) { A - returnmmobilenetworkinfo.isavailable (); - the } - - } - + return false; -}third, determine whether the mobile network is available
1 Public Static intGetconnectedtype (Context context) {2 3 if(Context! =NULL) { 4 5Connectivitymanager Mconnectivitymanager =(connectivitymanager) Context6 . Getsystemservice (context.connectivity_service);7 8Networkinfo Mnetworkinfo =mconnectivitymanager.getactivenetworkinfo ();9 Ten if(Mnetworkinfo! =NULL&&mnetworkinfo.isavailable ()) { One A returnMnetworkinfo.gettype (); - - } the - } - - return-1; +}four. Gets the type information for the current network connection
When developing an Android app, it involves network access, often requiring network status checks to provide users with the necessary reminders. This work can generally be done through Connectivitymanager.
Connectivitymanager has four main tasks :
1. Monitor mobile network status (including Gprs,wifi, UMTS, etc.)
2, when the mobile phone status changes, send broadcast
3. Failover when a network connection fails
4. Provide the application with a high-precision and coarse state that can obtain available networks
When we want to listen to the network status in the program, just a few steps:
1, define a receiver overloaded with the OnReceive function, in which to complete the required functions, such as based on WiFi and GPRS is disconnected to change the appearance of the space
1Connectionreceiver =NewBroadcastreceiver () {2 @Override3 Public voidOnReceive (Context context, Intent Intent) {4Connectivitymanager connectmgr =(Connectivitymanager) Getsystemservice (connectivity_service);5Networkinfo Mobnetinfo =Connectmgr.getnetworkinfo (connectivitymanager.type_mobile);6Networkinfo Wifinetinfo =Connectmgr.getnetworkinfo (Connectivitymanager.type_wifi);7 if(!mobnetinfo.isconnected () &&!wifinetinfo.isconnected ()) { 8LOG.I (TAG, "Unconnect"); 9 //Unconnect NetworkTen}Else { One //Connect Network A } - } -};View Code
2, register receiver in the appropriate place, you can register in the program, call the following function in OnCreate:
1 New 23 registerreceiver (Connectionreceiver, Intentfilter);
View Code
3, when appropriate to unregister receiver, can be canceled in the program, in the Ondestroye call the following function:
1 if NULL 23 }
View Code
Determine if network connectivity is available and monitor network status in Android