赢得互联网项目名称二G,判断用户二G

Android 判断用户二G/3G/4G移动数据互联网

在做 Android App
的时候,为了给用户省流量,为了不刺激用户的气愤,为了更好的用户体验,是要求基于用户日前互连网状态来做一些调整的,也能够在
App 的装置模块里,让用户自身选用,在 二G / 三G / 四G
互联网条件下,是还是不是允许请求一些流量相比较大的数码。

通过 Android 提供的 TelephonyManager 和 ConnectivityManager 都得以获取到
NetworksInfo 对象,能够透过 getType() 获取项目,判断是 wifi 依然 mobile
,要是是 mobile ,能够由此 NetworksInfo 对象的 getSubType() 和
getSubTypeName() 能够取得到对于的互连网项目和名字。

互连网项目和名字定义在 TelephonyManager 类里。

/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;

见到那一个代码和注释,相信未有那上面知识的人很丢脸懂,都什么东西?那注释跟没注释有吗差别?!就是令人瞧着越来越闹心而已。所以说,注释对阅读代码的人很要紧。当然那么些东西恐怕太规范了,写那几个代码的人臆想是想写也不知道该怎么了,得写多大学一年级坨啊?!作者在结尾会贴上有个别自己收十的材料,能够供大家参考一下,不是很详细,也半间不界,正是大约有个印象。

TelephonyManager 还提供了 getNetworkTypeName(int type)
的不二等秘书籍,那么些措施可以回去1个字符串,可是新闻量十分的小。

这怎么判断是 二G , 三G 依旧 四G 互联网呢?TelephonyManager
还提供了其它三个方式,getNetworkClass(int networkType)
,但这些艺术被埋伏掉了,小编把代码贴一下。

public static int getNetworkClass(int networkType) {
    switch (networkType) {
        case NETWORK_TYPE_GPRS:
        case NETWORK_TYPE_EDGE:
        case NETWORK_TYPE_CDMA:
        case NETWORK_TYPE_1xRTT:
        case NETWORK_TYPE_IDEN:
    return NETWORK_CLASS_2_G;
        case NETWORK_TYPE_UMTS:
        case NETWORK_TYPE_EVDO_0:
        case NETWORK_TYPE_EVDO_A:
        case NETWORK_TYPE_HSDPA:
        case NETWORK_TYPE_HSUPA:
        case NETWORK_TYPE_HSPA:
        case NETWORK_TYPE_EVDO_B:
        case NETWORK_TYPE_EHRPD:
        case NETWORK_TYPE_HSPAP:
    return NETWORK_CLASS_3_G;
        case NETWORK_TYPE_LTE:
    return NETWORK_CLASS_4_G;
        default:
    return NETWORK_CLASS_UNKNOWN;
    }
}

  然后上面是那多少个常量的值。

/** Unknown network class. {@hide} */
public static final int NETWORK_CLASS_UNKNOWN = 0;
/** Class of broadly defined "2G" networks. {@hide} */
public static final int NETWORK_CLASS_2_G = 1;
/** Class of broadly defined "3G" networks. {@hide} */
public static final int NETWORK_CLASS_3_G = 2;
/** Class of broadly defined "4G" networks. {@hide} */
public static final int NETWORK_CLASS_4_G = 3;

  

不知情怎么要把那些东西给隐蔽起来,然道是不可信赖?!依旧别的的更好的方法?!不驾驭,先这么呢,未来透过上边包车型地铁一手,是能够领略用户用的是什么样网络,当然也足以分别出来用户使用的是
二G , 三G 照旧 4G
了。当然,你获得到那些数据后,你也足以推算出用户用的是哪家店铺的互连网,移动的,联通的,依然邮电通讯的,当然,只在炎黄。而且虚拟运营商初步真的上市后,那么些就分别不出来是京东的,依然国美,苏宁的了,不过你能够精晓你的手提式有线电话机号用的是联通的网依旧移动的网。

末尾贴上笔者收集整理的有的资料,能够参照一下。

  • GPRS : 2G(2.5) General Packet Radia Service 114kbps
  • EDGE : 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
  • UMTS : 三G WCDMA 联通叁G Universal Mobile Telecommunication System
    完整的三G移动通信技术标准
  • CDMA : 2G 邮电通讯 Code Division Multiple Access 码分多址
  • EVDO_0 : 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution – Data Only
    (Data Optimized) 153.6kps – 2.4mbps 属于3G
  • EVDO_A : 3G 1.8mbps – 3.1mbps 属于3G过渡,3.5G
  • 一x科雷傲TT : 2G CDMA2000 1x智跑TT (凯雷德TT – 有线电传输技术) 14四kbps 二G的衔接,
  • HSDPA : 叁.5G 高速下行分组接入 3.5G WCDMA High Speed Downlink Packet
    Access 1四.四mbps
  • HSUPA : 三.五G High Speed Uplink Packet Access 高速上行链路分组接入
    一.四 – 伍.八 mbps
  • HSPA : 3G (分HSDPA,HSUPA) High Speed Packet Access
  • IDEN : 贰G Integrated Dispatch Enhanced Networks 集成数字增强型互连网(属于二G,来自维基百科)
  • EVDO_B : 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
  • LTE : 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE
    Advanced 才是4G
  • EH途锐PD : 3G CDMA三千向LTE 四G的高级中学级产物 埃沃lved High Rate Packet Data
    H路虎极光PD的晋级
  • HSPAP : 3G HSPAP 比 HSDPA 快些

有对象说她是运动 4G 的网络,获取到的是
一七。于是小编又去翻了瞬间源码,找到了如下的代码。也正是说当有新的连串被帮衬的时候,Android
会扩充对于的品类代码。确实 一七 是 NETWO奥迪Q7KTYPETD_SCDMA
也便是运动的那1坨。别的的代码就不贴了,差不多正是以此意思。上边包车型大巴代码是5.壹的代码。

/** Network type is unknown */
public static final int NETWORK_TYPE_UNKNOWN = 0;
/** Current network is GPRS */
public static final int NETWORK_TYPE_GPRS = 1;
/** Current network is EDGE */
public static final int NETWORK_TYPE_EDGE = 2;
/** Current network is UMTS */
public static final int NETWORK_TYPE_UMTS = 3;
/** Current network is CDMA: Either IS95A or IS95B*/
public static final int NETWORK_TYPE_CDMA = 4;
/** Current network is EVDO revision 0*/
public static final int NETWORK_TYPE_EVDO_0 = 5;
/** Current network is EVDO revision A*/
public static final int NETWORK_TYPE_EVDO_A = 6;
/** Current network is 1xRTT*/
public static final int NETWORK_TYPE_1xRTT = 7;
/** Current network is HSDPA */
public static final int NETWORK_TYPE_HSDPA = 8;
/** Current network is HSUPA */
public static final int NETWORK_TYPE_HSUPA = 9;
/** Current network is HSPA */
public static final int NETWORK_TYPE_HSPA = 10;
/** Current network is iDen */
public static final int NETWORK_TYPE_IDEN = 11;
/** Current network is EVDO revision B*/
public static final int NETWORK_TYPE_EVDO_B = 12;
/** Current network is LTE */
public static final int NETWORK_TYPE_LTE = 13;
/** Current network is eHRPD */
public static final int NETWORK_TYPE_EHRPD = 14;
/** Current network is HSPA+ */
public static final int NETWORK_TYPE_HSPAP = 15;
/** Current network is GSM {@hide} */
public static final int NETWORK_TYPE_GSM = 16;
 /** Current network is TD_SCDMA {@hide} */
public static final int NETWORK_TYPE_TD_SCDMA = 17;
/** Current network is IWLAN {@hide} */
public static final int NETWORK_TYPE_IWLAN = 18;

  示例:

////////
    /*
     * 前边注释和后边注释是后来加的判断网络类型
     */
    //WIFI("wifi", 1), G2("2G", 2), G3("3G", 3), WAP("wap", 4), NO("invalid", 5), ALL(
    //"all", 6), G4("4G",7);
    private static final int NETWORK_TYPE_UNAVAILABLE = 5;  
        // private static final int NETWORK_TYPE_MOBILE = -100;  
    private static final int NETWORK_TYPE_WIFI = 1;  

    private static final int NETWORK_CLASS_WIFI = 1;  
    private static final int NETWORK_CLASS_UNAVAILABLE = 5;  
        /** Unknown network class. */  
    private static final int NETWORK_CLASS_UNKNOWN = 6;  
        /** Class of broadly defined "2G" networks. */  
        private static final int NETWORK_CLASS_2_G = 2;  
        /** Class of broadly defined "3G" networks. */  
        private static final int NETWORK_CLASS_3_G = 3;  
        /** Class of broadly defined "4G" networks. */  
        private static final int NETWORK_CLASS_4_G = 7;  
    /** 
     * 获取网络类型 
     *  
     * @return 
     */  
    public String getCurrentNetworkType() {  
        int networkClass = getNetworkClass();  
        String type = "未知";  
        switch (networkClass) {  
        case NETWORK_CLASS_UNAVAILABLE:  
            type = "无";  
            break;  
        case NETWORK_CLASS_WIFI:  
            type = "Wi-Fi";  
            break;  
        case NETWORK_CLASS_2_G:  
            type = "2G";
            break;  
        case NETWORK_CLASS_3_G:  
            type = "3G";  
            break;  
        case NETWORK_CLASS_4_G:  
            type = "4G";  
            break;  
        case NETWORK_CLASS_UNKNOWN:  
            type = "未知";  
            break;  
        }  
        return type;  
    }  

    private int getNetworkClassByType(int networkType) {  
        switch (networkType) {  
        case TelephonyManager.NETWORK_TYPE_GPRS:  
        case TelephonyManager.NETWORK_TYPE_EDGE:  
        case TelephonyManager.NETWORK_TYPE_CDMA:  
        case TelephonyManager.NETWORK_TYPE_1xRTT:  
        case TelephonyManager.NETWORK_TYPE_IDEN:  
            return NETWORK_CLASS_2_G;  
        case TelephonyManager.NETWORK_TYPE_UMTS:  
        case TelephonyManager.NETWORK_TYPE_EVDO_0:  
        case TelephonyManager.NETWORK_TYPE_EVDO_A:  
        case TelephonyManager.NETWORK_TYPE_HSDPA:  
        case TelephonyManager.NETWORK_TYPE_HSUPA:  
        case TelephonyManager.NETWORK_TYPE_HSPA:  
        case TelephonyManager.NETWORK_TYPE_EVDO_B:  
        case TelephonyManager.NETWORK_TYPE_EHRPD:  
        case TelephonyManager.NETWORK_TYPE_HSPAP:  
            return NETWORK_CLASS_3_G;  
        case TelephonyManager.NETWORK_TYPE_LTE:  
            return NETWORK_CLASS_4_G;  
        default:  
            return NETWORK_CLASS_UNKNOWN;  
        }  
    }  
    private int getNetworkClass() {  

        connectivityManager = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); 
        networkInfo = connectivityManager.getActiveNetworkInfo(); 

            if (networkInfo != null&& networkInfo.isConnected()) {  
                int type = networkInfo.getType();  
                if (type == ConnectivityManager.TYPE_WIFI) {  
                    mNetWorkType =  NetEnum.WIFI.getID();
                } else if (type == ConnectivityManager.TYPE_MOBILE) {  
                    TelephonyManager telephonyManager = (TelephonyManager) this.getSystemService(  
                                    Context.TELEPHONY_SERVICE);  
                    mNetWorkType = getNetworkClassByType(telephonyManager.getNetworkType());
                }  
            } else {  
                mNetWorkType = NETWORK_TYPE_UNAVAILABLE;  
            }  
         return mNetWorkType;
        } 

    ////

  参报考博士大学生客:http://blog.binkery.com/android/networks_type.html

http://www.eoeandroid.com/thread-540327-1-1.html?_dsign=77a59737

近期在类型中用到啦,总计了下。。。。一大半都是外人总计好的,就径直拿过来啊,哈哈。。

率先工程Manifest文件要引用:

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

//获取网络服务

 1 public static String getNetworkTypeName(Context context) {
 2      if (context != null) {
 3           ConnectivityManager connectMgr = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
 4           if (connectMgr != null) {
 5                   NetworkInfo info = connectMgr.getActiveNetworkInfo();
 6                   if (info != null) {
 7                        switch (info.getType()) {
 8                             case ConnectivityManager.TYPE_WIFI:
 9                                   return "WIFI";
10                             case ConnectivityManager.TYPE_MOBILE:
11                                   return getNetworkTypeName(info.getSubtype());
12                        }
13                   }
14           }
15      }
16      return getNetworkTypeName(TelephonyManager.NETWORK_TYPE_UNKNOWN);
17 }

//互连网型号

 1 public static String getNetworkTypeName(int type) {
 2      switch (type) {
 3           case TelephonyManager.NETWORK_TYPE_GPRS:
 4                return "GPRS";
 5           case TelephonyManager.NETWORK_TYPE_EDGE:
 6                return "EDGE";
 7           case TelephonyManager.NETWORK_TYPE_UMTS:
 8                return "UMTS";
 9           case TelephonyManager.NETWORK_TYPE_HSDPA:
10                return "HSDPA";
11           case TelephonyManager.NETWORK_TYPE_HSUPA:
12                return "HSUPA";
13           case TelephonyManager.NETWORK_TYPE_HSPA:
14                return "HSPA";
15           case TelephonyManager.NETWORK_TYPE_CDMA:
16                return "CDMA";
17           case TelephonyManager.NETWORK_TYPE_EVDO_0:
18                return "CDMA - EvDo rev. 0";
19           case TelephonyManager.NETWORK_TYPE_EVDO_A:
20                return "CDMA - EvDo rev. A";
21           case TelephonyManager.NETWORK_TYPE_EVDO_B:
22                return "CDMA - EvDo rev. B";
23           case TelephonyManager.NETWORK_TYPE_1xRTT:
24                return "CDMA - 1xRTT";
25           case TelephonyManager.NETWORK_TYPE_LTE:
26                return "LTE";
27           case TelephonyManager.NETWORK_TYPE_EHRPD:
28                return "CDMA - eHRPD";
29           case TelephonyManager.NETWORK_TYPE_IDEN:
30                return "iDEN";
31           case TelephonyManager.NETWORK_TYPE_HSPAP:
32                return "HSPA+";
33           default:
34                return "UNKNOWN";
35      }
36 }

调用:

 1 @Override
 2     public void onClick(View view) {
 3         // TODO Auto-generated method stub
 4         switch (view.getId()) {
 5         case R.id.btn_main:
 6              String type =getNetworkTypeName(context);
 7              if (type!=null){
 8                  if ( type.equals("GPRS") || type.equals("EGDE") )
 9                    type ="2G";
10                  else      if (type.equals("CDMA"))
11                        type ="电信 2G";
12                  else if (type.equals("EVDO"))
13                        type ="电信 3G";
14                  else if (type.equals("LTE"))
15                        type ="电信 4G";
16                  else if (type.equals("HSDPA") ||type.equals("UMTS")  )
17                        type ="3G";       
18                  
19              }
20             btn_main.setText(type);
21             break;
22         }

注:

运动或联通的叁G为UMTS或HSDPA,移动和联通的二G为GP奥迪Q7S或EGDE,邮电通讯的2G为CDMA,邮电通讯

的3G为EVDO,电信4G LTE

 

能够参考资料:

GPRS 2G(2.5) General Packet Radia Service 114kbps
EDGE 2G(2.75G) Enhanced Data Rate for GSM Evolution 384kbps
UMTS 三G WCDMA 联通3G Universal Mobile Telecommunication System
完整的3G移动通信技术标准
CDMA 2G 邮电通讯 Code Division Multiple Access 码分多址
EVDO_0 3G (EVDO 全程 CDMA2000 1xEV-DO) Evolution – Data Only (Data
Optimized) 153.6kps – 2.4mbps 属于3G
EVDO_A 3G 1.8mbps – 3.1mbps 属于3G过渡,3.5G
1xRTT 二G CDMA两千 壹xRTT (大切诺基TT – 有线电传输技术) 14肆kbps 二G的连结,
HSDPA 三.五G 高速下行分组接入 3.伍G WCDMA High Speed Downlink Packet Access
14.四mbps 
HSUPA 三.5G High Speed Uplink Packet Access 高速上行链路分组接入 壹.四 –
5.捌 mbps
HSPA 3G (分HSDPA,HSUPA) High Speed Packet Access 
IDEN 二G Integrated Dispatch Enhanced Networks 集成数字增强型网络(属于二G,来自维基百科)
EVDO_B 3G EV-DO Rev.B 14.7Mbps 下行 3.5G
LTE 4G Long Term Evolution FDD-LTE 和 TDD-LTE , 3G过渡,升级版 LTE
Advanced 才是4G 
EH奥迪Q三PD 叁G CDMA2000向LTE 肆G的中游产物 埃沃lved High Rate Packet Data
H奥德赛PD的升官
HSPAP 3G HSPAP 比 HSDPA