Android android gets the network status, Android android gets

Source: Internet
Author: User

Android android gets the network status, Android android gets

First, add the permission in AndroidManifest. xml.

<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"/>

1. Determine whether a network connection exists.

public 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;  }

2. Determine whether the Wi-Fi network is available

public boolean isWifiConnected(Context context) {      if (context != null) {          ConnectivityManager mConnectivityManager = (ConnectivityManager) context                  .getSystemService(Context.CONNECTIVITY_SERVICE);          NetworkInfo mWiFiNetworkInfo = mConnectivityManager                  .getNetworkInfo(ConnectivityManager.TYPE_WIFI);          if (mWiFiNetworkInfo != null) {              return mWiFiNetworkInfo.isAvailable();          }      }      return false;  }

3. Determine whether the MOBILE network is available

public boolean isMobileConnected(Context context) {      if (context != null) {          ConnectivityManager mConnectivityManager = (ConnectivityManager) context                  .getSystemService(Context.CONNECTIVITY_SERVICE);          NetworkInfo mMobileNetworkInfo = mConnectivityManager                  .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);          if (mMobileNetworkInfo != null) {              return mMobileNetworkInfo.isAvailable();          }      }      return false;  }

4. Determine the network type

// Return value-1: No network 1: WIFI network 2: wap network 3: net network public static int GetNetype (Context context) {int netType =-1; connectivityManager connMgr = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connMgr. getActiveNetworkInfo (); if (networkInfo = null) {return netType;} int nType = networkInfo. getType (); if (nType = ConnectivityManager. TYPE_MOBILE) {if (networkInfo. getExtraInfo (). toLowerCase (). equals ("cmnet") {netType = 3 ;}else {netType = 2 ;}} else if (nType = ConnectivityManager. TYPE_WIFI) {netType = 1;} return netType ;}


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.