Android Practice-Monitor network status

Source: Internet
Author: User

Android Monitor network status     When we use an Android phone, some apps need a network environment to run, so the phone needsavailableNetwork, whether it is 2G, 3G or WiFi, even some of the more consumption of the app can only operate in the WiFi environment, or provide users to choose whether only in the WiFi environment, as the user seems the latter experience better, then as a developer, we just need a few steps to give users this experience.----------------------------------------------------------------------------------------------------------- ------------------1. Add Permissions :The app needs to be networked and requires access to 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, can distinguish between access and general access.
/** * Check if WiFi is connected * * @author Ysjian * @date 2014-5-9 * @return If connected 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 If 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 The presence of WiFi and cellular data any available network returns TRUE, otherwise returns false */public Boolean Hasactiv Itynetwork () {Connectivitymanager Connectivitymanager = (connectivitymanager) getsystemservice (Context.CONNECTIVITY _service); return ConnectivitymAnager.getactivenetworkinfo ()! = NULL;} 
3. Jump Settings: If there is no network, jump to the network settings interface, The network status can be judged by invoking one of the three methods above according to the specific 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

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.