Some common functions of Android to Judge network status

Source: Internet
Author: User

Determines whether there is a network connection public static Boolean isnetworkconnected (context context) {if (context! = null) {Co Nnectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (Context.connec            Tivity_service);            Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();            if (mnetworkinfo! = null) {return mnetworkinfo.isavailable ();    }} return false;            }//Determine if the WiFi network is available public static Boolean iswificonnected (context context) {if (context! = null) { Connectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (context.c            Onnectivity_service);            Networkinfo mwifinetworkinfo = Mconnectivitymanager. Getnetworkinfo (Connectivitymanager.type_wifi);            if (mwifinetworkinfo! = null) {return mwifinetworkinfo.isavailable ();       }        } return false;            }//Determine if the mobile network is available public static Boolean ismobileconnected (context context) {if (context! = null) { Connectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (Conte Xt.            Connectivity_service);             Networkinfo mmobilenetworkinfo = Mconnectivitymanager. Getnetworkinfo (Connectivitymanager.type_mobile);            if (mmobilenetworkinfo! = null) {return mmobilenetworkinfo.isavailable ();    }} return false;            }//Gets the type information of the current network connection public static int Getconnectedtype (context context) {if (context! = null) { Connectivitymanager Mconnectivitymanager = (connectivitymanager) context. Getsystemservice (Context.con            Nectivity_service);            Networkinfo mnetworkinfo = Mconnectivitymanager.getactivenetworkinfo ();        if (mnetworkinfo! = null && mnetworkinfo.isavailable ()) {        return Mnetworkinfo.gettype ();    }} return-1; }

Some common functions of Android to Judge 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.