Android Monitor network status When we use an Android phone, some apps need to be implemented in a network environment, so the phone needsavailableNetwork, whether it's 2G, 3G, or WiFi. Even some of the more mobile apps can only be carried out in the context of WiFi, or if the user chooses to do so only in a WiFi environment, as the user looks like the latter experience is better. So as a developer, we just need a few steps to give the user such an experience.----------------------------------------------------------------------------------------------------------- ------------------1. permission to join:the app needs to be networked. And the need to access the network status, so add permissions in Androidmanifest.xml.
<uses-permission android:name= "Android.permission.INTERNET"/> <uses-permission android:name= " Android.permission.ACCESS_NETWORK_STATE "/>
2. Status acquisition: mobile network has data (2g/3g/4g) connection, WiFi connection two states, to distinguish between access and general access.
/** * Check if WiFi is connected * * @author Ysjian * @date 2014-5-9 * @return Assuming the connection returns TRUE, otherwise returns false */public Boolean iswificonnected () { Connectivitymanager Connectivitymanager = (connectivitymanager) getsystemservice (Context.connectivity_service); Networkinfo wifiinfo = Connectivitymanager.getnetworkinfo (Connectivitymanager.type_wifi); return wifiInfo! = NULL; /** * Check if the cellular network (4G/3G/2G) is connected * * @author Ysjian * @date 2014-5-9 * @return Assuming that the connection returns true, otherwise false */public Boolean Ismobilen Etworkconnected () {Connectivitymanager Connectivitymanager = (connectivitymanager) getsystemservice ( Context.connectivity_service); Networkinfo mobilenetworkinfo = Connectivitymanager.getnetworkinfo (connectivitymanager.type_mobile); return Mobilenetworkinfo! = NULL;} /** * Check if there is a network available * * @author Ysjian * @date 2014-5-9 * @return There is free WiFi and mobile data available on the network return true. Otherwise returns false */public Boolean hasactivitynetwork () {Connectivitymanager Connectivitymanager = (Connectivitymanager) Getsystemservice (Context.connectivity_service); return ConnectivitymAnager.getactivenetworkinfo ()! = NULL;} 3. Jump settings: Assume that there is no network, jump to the network settings interface. The network status can be inferred from one of the three methods above, based on detailed requirements.
if (!hasactivitynetwork ()) {Alertdialog.builder Builder = new Alertdialog.builder (this); Builder. Settitle ("No Network Available"). Setmessage ("Do you want to set the network?") . Setpositivebutton ("Yes", new Dialoginterface.onclicklistener () { @Override public void OnClick (Dialoginterface dialog,int which) { Intent Intent = null; Intent = newintent (Android.provider.Settings.ACTION_WIRELESS_SETTINGS); StartActivity (Intent); }}). Setnegativebutton ("No", new Dialoginterface.onclic Klistener () {@Override public void OnClick (dialogint Erface dialog, int which) { Dialog.dismiss (); }}). Show ();}Article Source: http://blog.csdn.net/ysjian_pingcx/article/details/25461053
Android Practice-Monitor network status