Android 開發工具類 18_NetWorkUtil

來源:互聯網
上載者:User

標籤:

檢測網路的一個工具包:

1、網路是否可用;

2、判斷是否有網路連接;

3、判斷 WIFI 網路是否可用;

4、判斷 MOBILE 網路是否可用;

5、擷取當前網路連接的類型資訊;

6、擷取當前的網路狀態 -1:沒有網路 1:WIFI網路2:wap 網路3:net網路;

  1 import android.content.Context;  2 import android.net.ConnectivityManager;  3 import android.net.NetworkInfo;  4   5 /**  6  * @Title NetWorkUtil  7  * @Description 是檢測網路的一個工具包  8  * @author 淡定  9  */ 10 public class NetWorkUtil { 11      12     public static enum NetType { 13         WIFI, CMNET, CMWAP, NoneNet 14     } 15  16     /** 17      * 網路是否可用 18      *  19      * @param context 20      * @return 21      */ 22     public static boolean isNetworkAvailable(Context context) { 23          24         ConnectivityManager mgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 25         NetworkInfo[] info = mgr.getAllNetworkInfo(); 26         if (info != null) { 27             for (int i = 0; i < info.length; i++) { 28                 if (info[i].getState() == NetworkInfo.State.CONNECTED) { 29                     return true; 30                 } 31             } 32         } 33         return false; 34     } 35  36     /** 37      * 判斷是否有網路連接 38      *  39      * @param context 40      * @return 41      */ 42     public static boolean isNetworkConnected(Context context) { 43         if (context != null) { 44             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 45             NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 46             if (mNetworkInfo != null) { 47                 return mNetworkInfo.isAvailable(); 48             } 49         } 50         return false; 51     } 52  53     /** 54      * 判斷WIFI網路是否可用 55      *  56      * @param context 57      * @return 58      */ 59     public static boolean isWifiConnected(Context context) { 60          61         if (context != null) { 62             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 63             NetworkInfo mWiFiNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); 64             if (mWiFiNetworkInfo != null) { 65                 return mWiFiNetworkInfo.isAvailable(); 66             } 67         } 68         return false; 69     } 70  71     /** 72      * 判斷 MOBILE 網路是否可用 73      *  74      * @param context 75      * @return 76      */ 77     public static boolean isMobileConnected(Context context) { 78         if (context != null) { 79             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 80             NetworkInfo mMobileNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); 81             if (mMobileNetworkInfo != null) { 82                 return mMobileNetworkInfo.isAvailable(); 83             } 84         } 85         return false; 86     } 87  88     /** 89      * 擷取當前網路連接的類型資訊 90      *  91      * @param context 92      * @return 93      */ 94     public static int getConnectedType(Context context) { 95          96         if (context != null) { 97             ConnectivityManager mConnectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); 98             NetworkInfo mNetworkInfo = mConnectivityManager.getActiveNetworkInfo(); 99             if (mNetworkInfo != null && mNetworkInfo.isAvailable()) {100                 return mNetworkInfo.getType();101             }102         }103         return -1;104     }105 106     /**107      * 擷取當前的網路狀態 -1:沒有網路 1:WIFI網路2:wap 網路3:net網路108      * 109      * @param context110      * @return111      */112     public static NetType getAPNType(Context context) {113         114         ConnectivityManager connMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);115         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();116         if (networkInfo == null) {117             return NetType.NoneNet;118         }119         int nType = networkInfo.getType();120 121         if (nType == ConnectivityManager.TYPE_MOBILE) {122             if (networkInfo.getExtraInfo().toLowerCase().equals("cmnet")) {123                 return NetType.CMNET;124             } else {125                 return NetType.CMWAP;126             }127         } else if (nType == ConnectivityManager.TYPE_WIFI) {128             return NetType.WIFI;129         }130         return NetType.NoneNet;131 132     }133 }

 

Android 開發工具類 18_NetWorkUtil

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.