zoukankan      html  css  js  c++  java
  • 获取网络状态

    首先在AndroidManifest.xml添加权限

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

    1,判断是否有网络连接

     1     public boolean isNetworkConnected(Context context) {
     2         if (context != null) {
     3             ConnectivityManager mConnectivityManager = (ConnectivityManager) context
     4                     .getSystemService(Context.CONNECTIVITY_SERVICE);
     5             NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo();
     6             if (mNetworkInfo != null) {
     7                 return mNetworkInfo.isAvailable();
     8             }
     9         }
    10 
    11         return false;
    12     }

    2.判断WIFI网络是否可用

     1     public boolean isWifiConnected(Context context) {
     2         if (context != null) {
     3             ConnectivityManager mConnectivityManager = (ConnectivityManager) context
     4                     .getSystemService(Context.CONNECTIVITY_SERVICE);
     5             NetworkInfo mWiFiNetworkInfo = mConnectivityManager
     6                     .getNetworkInfo(ConnectivityManager.TYPE_WIFI);
     7             if (mWiFiNetworkInfo != null) {
     8                 return mWiFiNetworkInfo.isAvailable();
     9             }
    10         }
    11         return false;
    12     }

    3.判断MOBILE网络是否可用

     1     public boolean isMobileConnected(Context context) {
     2         if (context != null) {
     3             ConnectivityManager mConnectivityManager = (ConnectivityManager) context
     4                     .getSystemService(Context.CONNECTIVITY_SERVICE);
     5             NetworkInfo mMobileNetworkInfo = mConnectivityManager
     6                     .getNetworkInfo(ConnectivityManager.TYPE_MOBILE);
     7             if (mMobileNetworkInfo != null) {
     8                 return mMobileNetworkInfo.isAvailable();
     9             }
    10         }
    11         return false;
    12     }


    4.判断网络类型

     1  //返回值 -1:没有网络  1:WIFI网络2:wap网络3:net网络  
     2     public static int GetNetype(Context context) {
     3         int netType = -1;
     4         ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
     5         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
     6         if (networkInfo == null) {
     7             return netType;
     8         }
     9         int nType = networkInfo.getType();
    10         if (nType == ConnectivityManager.TYPE_MOBILE) {
    11             if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {
    12                 netType = 3;
    13             } else {
    14                 netType = 2;
    15             }
    16         } else if (nType == ConnectivityManager.TYPE_WIFI) {
    17             netType = 1;
    18         }
    19         return netType;
    20     }
  • 相关阅读:
    多线程与MySQL(十)
    多进程与多线程(九)
    异常处理与网络编程(八)
    面向对象,绑定方法与异常处理(七)
    模块与对象(六)
    包与模块(五)
    迭代器与函数Python学习(四)
    函数与装饰器Python学习(三)
    数据库
    并发编程
  • 原文地址:https://www.cnblogs.com/lizhanqi/p/5715362.html
Copyright © 2011-2022 走看看