Android Practice-Monitor network status

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.