Android determines network connection and android Network Connection

Source: Internet
Author: User

Android determines network connection and android Network Connection

1 import android. content. context; 2 import android.net. connectivityManager; 3 import android.net. networkInfo; 4 5/** 6 * network tool to add a network status license: <uses-permission 7 * android: name = "android. permission. ACCESS_NETWORK_STATE "/> 8*9 * @ author benny10 * 11 */12 public class NetworkUtil {13 14/** 15 * network connection status 16 * <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE "/> 17 * @ param context18 * program upstream/downstream 19 * @ return true: 20 */21 public static boolean isNetworkConnected (Context context) {22 if (context! = Null) {23 ConnectivityManager connectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); 24 25 NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo (); 26 if (networkInfo! = Null) {27 return networkInfo. isAvailable (); 28} 29} 30 return false; 31} 32 33/** 34 * WiFi network connection status 35 * <uses-permission android: name = "android. permission. ACCESS_WIFI_STATE "/> 36 * @ param context37 * program Context 38 * @ return true: 39 */40 public static boolean isWifiConnected (context) {41 if (Context! = Null) {42 ConnectivityManager connectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); 43 44 NetworkInfo networkInfo = connectivityManager. getNetworkInfo (ConnectivityManager. TYPE_WIFI); 45 if (networkInfo! = Null) {46 return networkInfo. isAvailable (); 47} 48} 49 return false; 50} 51 52/** 53 * Mobile network connection status 54 * <uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE "/> 55*56 * @ param context57 * program Context 58 * @ return true: 59 */60 public static boolean isMobileConnected (context) {61 if (Context! = Null) {62 ConnectivityManager connectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); 63 64 NetworkInfo networkInfo = connectivityManager. getActiveNetworkInfo (); 65 if (networkInfo! = Null & networkInfo. getType () = ConnectivityManager. TYPE_MOBILE) {66 return networkInfo. isAvailable (); 67} 68} 69 return false; 70} 71 72/** 73 * Get network connection type 74 * @ param context 75 * @ return ConnectivityManager. TYPE_XXXX76 */77 public static int getConnectedType (Context context) {78 if (context! = Null) {79 ConnectivityManager connectivityManager = (ConnectivityManager) context. getSystemService (Context. CONNECTIVITY_SERVICE); 80 NetworkInfo mNetworkInfo = connectivityManager. getActiveNetworkInfo (); 81 if (mNetworkInfo! = Null & mNetworkInfo. isAvailable () {82 return mNetworkInfo. getType (); 83} 84} 85 return-1; 86} 87 88}

 

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.