android開發擷取網路狀態,wifi,wap,2g,3g.工具類(一)

來源:互聯網
上載者:User

標籤:

android開發擷取網路狀態整理:

 1 package com.gzcivil.utils; 2  3 import android.content.Context; 4 import android.net.ConnectivityManager; 5 import android.net.NetworkInfo; 6 import android.telephony.TelephonyManager; 7 import android.text.TextUtils; 8  9 public class NetworkUtils {10 11     /** 沒有網路 */12     public static final int NETWORKTYPE_INVALID = 0;13     /** wap網路 */14     public static final int NETWORKTYPE_WAP = 1;15     /** 2G網路 */16     public static final int NETWORKTYPE_2G = 2;17     /** 3G和3G以上網路,或統稱為快速網路 */18     public static final int NETWORKTYPE_3G = 3;19     /** USB網路 */20     public static final int NETWORKTYPE_USB = 4;21     /** wifi網路 */22     public static final int NETWORKTYPE_WIFI = 5;23 24     /**25      * 擷取網路狀態,wifi,wap,2g,3g.26      * 27      * @param context28      *            上下文29      * @return int 網路狀態 {@link #NETWORKTYPE_2G},{@link #NETWORKTYPE_3G}, *30      *         {@link #NETWORKTYPE_INVALID},{@link #NETWORKTYPE_WAP}*31      *         <p>32      *         {@link #NETWORKTYPE_WIFI}33      */34     public static int getNetWorkType(Context context) {35         ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);36         NetworkInfo networkInfo = manager.getActiveNetworkInfo();37         int mNetWorkType = 0;38         if (networkInfo != null && networkInfo.isConnected()) {39             String type = networkInfo.getTypeName();40             if (type.equalsIgnoreCase("WIFI")) {41                 mNetWorkType = NETWORKTYPE_WIFI;42             } else if (type.equalsIgnoreCase("usb")) {43                 mNetWorkType = NETWORKTYPE_USB;44             } else if (type.equalsIgnoreCase("MOBILE")) {45                 @SuppressWarnings("deprecation")46                 String proxyHost = android.net.Proxy.getDefaultHost();47                 mNetWorkType = TextUtils.isEmpty(proxyHost) ? (isFastMobileNetwork(context) ? NETWORKTYPE_3G : NETWORKTYPE_2G) : NETWORKTYPE_WAP;48             }49         } else {50             mNetWorkType = NETWORKTYPE_INVALID;51         }52         return mNetWorkType;53     }54     55     private static boolean isFastMobileNetwork(Context context) {56         TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);57         switch (telephonyManager.getNetworkType()) {58         case TelephonyManager.NETWORK_TYPE_1xRTT:59             return false; // ~ 50-100 kbps60         case TelephonyManager.NETWORK_TYPE_CDMA:61             return false; // ~ 14-64 kbps62         case TelephonyManager.NETWORK_TYPE_EDGE:63             return false; // ~ 50-100 kbps64         case TelephonyManager.NETWORK_TYPE_EVDO_0:65             return true; // ~ 400-1000 kbps66         case TelephonyManager.NETWORK_TYPE_EVDO_A:67             return true; // ~ 600-1400 kbps68         case TelephonyManager.NETWORK_TYPE_GPRS:69             return false; // ~ 100 kbps70         case TelephonyManager.NETWORK_TYPE_HSDPA:71             return true; // ~ 2-14 Mbps72         case TelephonyManager.NETWORK_TYPE_HSPA:73             return true; // ~ 700-1700 kbps74         case TelephonyManager.NETWORK_TYPE_HSUPA:75             return true; // ~ 1-23 Mbps76         case TelephonyManager.NETWORK_TYPE_UMTS:77             return true; // ~ 400-7000 kbps78         case TelephonyManager.NETWORK_TYPE_EHRPD:79             return true; // ~ 1-2 Mbps80         case TelephonyManager.NETWORK_TYPE_EVDO_B:81             return true; // ~ 5 Mbps82         case TelephonyManager.NETWORK_TYPE_HSPAP:83             return true; // ~ 10-20 Mbps84         case TelephonyManager.NETWORK_TYPE_IDEN:85             return false; // ~25 kbps86         case TelephonyManager.NETWORK_TYPE_LTE:87             return true; // ~ 10+ Mbps88         case TelephonyManager.NETWORK_TYPE_UNKNOWN:89             return false;90         default:91             return false;92         }93     }94 }

 

android開發擷取網路狀態,wifi,wap,2g,3g.工具類(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.