Several common functions used by Android to determine the network status

Source: Internet
Author: User

Several common functions used by Android to determine the network status

// Determine whether there is a network connection public static boolean isNetworkConnected (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager. getActiveNetworkInfo (); if (mNetworkInfo! = Null) {return mNetworkInfo. isAvailable () ;}return false ;}// determine whether the Wi-Fi network is available in public static boolean isWifiConnected (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networking = mConnectivityManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI); if (mWiFiNetworkInfo! = Null) {return mWiFiNetworkInfo. isAvailable () ;}} return false;} // determine whether the MOBILE network is available in public static boolean isMobileConnected (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networking = mConnectivityManager. getNetworkInfo (ConnectivityManager. TYPE_MOBILE); if (mMobileNetworkInfo! = Null) {return mMobileNetworkInfo. isAvailable () ;}} return false;} // obtain the type information of the current network connection. public static int getConnectedType (Context context) {if (context! = Null) {ConnectivityManager mConnectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo mNetworkInfo = mConnectivityManager. getActiveNetworkInfo (); if (mNetworkInfo! = Null & mNetworkInfo. isAvailable () {return mNetworkInfo. getType () ;}} return-1 ;}

Related Article

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.