Android Network connection judgment and processing Obtaining network information requires the appropriate permissions to be added to the Androidmanifest.xml file. <uses-permission android:name= "Android.permission.ACCESS_NETWORK_STATE"/> 1) determine if there is a network connection copy code 1 Public Boolean isnetworkconnected (context context) { 2 if (context! = null) { &nbs P;3 Connectivitymanager Mconnectivitymanager = (connectivitymanager) context 4 &N Bsp Getsystemservice (Context.connectivity_service); 5 Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); 6 if (mnetworkinfo! = null) { 7 &NB Sp return mnetworkinfo.isavailable (); 8 } 9 } 10 return false; &NBSP;11} Copy code 2) determine if WiFi network is available copy code 1 public boolean iswificonnected (context context) { 2 if (context! = null) { 3 Connectivitymanager Mconne Ctivitymanager = (Connectivitymanager) context 4 . GetS Ystemservice (Context.connectivity_service); 5 Networkinfo mwifinetworkinfo = Mconnectivitymanager 6 Getnetworkinfo (Connectivitymanager.type_wifi); 7 if (mwifinetworkinfo! = null) { 8 return mwifinetworkinfo.isavailable (); 9 } 10 } 11 return false; &NBSP;12} Copy code &NBSP;3) determine if the mobile network is available copy code 1 public boolean ismobileconnected (context context) { 2 if (context! = null) { 3 Connectivitymanager Mconnectivitymanager = (Connectivitymanager) context 4 &NBSP ;. Getsystemservice (Context.connectivity_service); 5 Networkinfo mmobilenetworkinfo = Mconnectivitymanager 6 &NBS P Getnetworkinfo (connectivitymanager.type_mobile); 7 if (mmobilenetworkinfo! = null) { 8 &NBS P return mmobilenetworkinfo.isavailable (); 9 } 10 } 11 return false; &NBSP;12} Copy code 4) Gets the type information of the current network connection Copy code 1 public static int Getconnectedtype (context context) { 2 if (context! = null) { 3 Connectivitymanager Mconnecti Vitymanager = (Connectivitymanager) context 4 &NBSp Getsystemservice (Context.connectivity_service); 5 Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo (); 6 if (mnetworkinfo! = null && mnetworkinfo.isavailable ()) { 7 return Mnetworkinfo.gettype (); 8 9 } 10 return-1; &NBSP;11} Copy code when developing Android apps, 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 the status of mobile phone network (including Gprs,wifi, UMTS, etc.) 2, mobile phone status changes, send broadcast 3, when a network connection fails to failover 4, Provides high-precision and coarse-state access to available networks for applications when we want to listen to the network state in the program, just a few steps: 1, define a receiver overloaded with the OnReceive function, in which to complete the required functions, Change the appearance of the space based on WiFi and GPRS disconnect Copy Code connectionreceiver = new Broadcastreceiver () { @Override public void OnReceive (context context, Intent Intent) { ConnectivItymanager connectmgr = (connectivitymanager) getsystemservice (connectivity_service); NetworkInfo Mobnetinfo = Connectmgr.getnetworkinfo (connectivitymanager.type_mobile); Networkinfo wifiNetInfo = Connectmgr.getnetworkinfo (Connectivitymanager.type_wifi); if (!mobnetinfo.isconnected () &&! Wifinetinfo.isconnected ()) { &NBSP;LOG.I (TAG, "Unconnect"); //Unconnect network }else { //Connect network } } }; Copy Code 2, Register receiver in the appropriate place, you can register in the program, call the following function in OnCreate: intentfilter intentfilter = new Intentfilter (); Intentfilter.addaction (connectivitymanager.connectivity_action); registerreceiver (ConnectionReceiver, Intentfilter); 3, unregister receiver when appropriate, can be canceled in the program, call the following function in Ondestroye: if (connectionreceiver! = NULL) { unregisterreceiver (connectionreceiver); } ps: There's a lot more on the web about using TelephoNymanager method, the method is as follows (but I have tried several times have problems, such as each time the first time into an activity will automatically receive a network disconnection signal, each time the network state changes to receive multiple callbacks and status is incorrect. Do not know what to pay attention to the place, seek guidance! Copy Code final Telephonymanager mtelephonymgr = (telephonymanager) getsystemservice (Context.telephony_service); mtelephonymgr.listen (New Phonestatelistener () {@Overridepublic void ondataconnectionstatechanged (int state) { Switch (state) {Case telephonymanager.data_disconnected://network disconnected break;case telephonymanager.data_connecting:// The network is connecting Break;case telephonymanager.data_connected://network connection on break;}}, Phonestatelistener.listen_data_connection_ State); Copy code As for the second method, I did not try. The first way is still relatively good, if you want to hide the program in the background, it is recommended to open a service, the Broadcastreceiver registered in the service, but do not forget to unregister. In the test, a routing device that is currently connected to WiFi is turned off, but the program does not capture the Unconnect network, possibly because the mobile device is immediately connected to another routing device. android monitoring network Status The above method is to determine whether the network is connected to the code, return True indicates that there is a network, return false indicates no network. In the development of Android Web applications, it is often necessary to determine whether a network connection is available, so it is often important to listen for changes in network status. Android Network status monitoring can use Broadcastreceiver to receive network status changes in the broadcast, the implementation of the following: Copy code//Register Network Monitor intentfilter filter = new Intentfilter (); filter.addAction (connectivitymanager.connectivity_action); registerreceiver (mnetworkstatereceiver, filter); //in the activity of OnDestroy: ' unregisterreceiver (Mnetworkstatereceiver); Cancel listening Copying code many friends in Android development, will encounter the phone network type judgment, because on the current Android platform phone: There may be 4 in the status 1. No network (this may be due to phone downtime, network not open, bad signal, etc.) 2. Use WiFi 3.CMWAP (China Mobile agent) 4.CMNET Internet These four states, if there is no network, must not be able to request the Internet, if it is WAP need to add China Mobile agent for mobile phones, for mobile phones to add agents, please go to http://www.2cto.com/ Kf/201111/112100.html Here is an example of adding China Mobile agent! Here's how the network is judged: Copy code 1/** 2 3 * @author sky 4 5 * Email [email protected] 6 7 * qq:840950105 8 9 * get current network status -1: No network 1:wifi network 2:wap Network 3:net network 10 11 * @param context12 13 * @return14 15 &NBSP;*/&NBSP;16&NBSP;17 public static int Getapntype (context context) { 18 19 INT NetType =-1; 20 21 Connectivitymanager connmgr = (Connectivitymanager) Context.getsystemservice (Context.connectivity_service), 22 23 Networkinfo Networkinfo = Connmgr.getactivenetworkinfo (); 24 25 26 27 if (networkinfo==null) { 28 29 return Nettype;&nbs p;30 31 } 32 33 int nType = Networkinfo.gettype (); nbsp;34 35 if (ntype==connectivitymanager.type_mobile) { 36 37 LOG.E ("Networkinfo.getextrainfo ()", "Networkinfo.getextrainfo () is" + Networkinfo.getextrainfo ()) 38 39 &NBsp if (Networkinfo.getextrainfo (). toLowerCase () equals ("Cmnet")) { 40 41 NetType = cmnet; 42 43 } 44 45 & nbsp else{ 46 47 NetType = Cmwap; 48 49 } 50 51 } 52 53 ELSE if (Ntype==connectivitymanager.type_wifi) { 54 55 & nbsp NetType = wifi; 56 57 } 58 59 return NE ttype; 60 61 } copy code because the access is the service object, so this network state is always refreshed, so we just need to get the network state on it! Learning is to accumulate, hope to share with you
Android Network connection determination and processing