zoukankan      html  css  js  c++  java
  • Android网络类型判断(2g、3g、wifi)

    判断网络类型是wifi,还是3G,还是2G网络,对不同
    的网络进行不同的处理,现将判断方法整理给大家,以供参考
     
    说明:下面用到的数据移动2G,联通2G,联通3G,wifi我都已经测试过,暂时手上
    没有电信的卡,所以没有验证,有电信手机的同事,可以验证一下,验证后将结果
    发送给大家。
     
     ConnectivityManager connectMgr = (ConnectivityManager) this
            .getSystemService(Context.CONNECTIVITY_SERVICE);
     
     NetworkInfo info = connectMgr.getActiveNetworkInfo();
     
    一、判断网络是否是wifi,在判断之前一定要进行的非空判断,如果没有任何网络
    连接info ==null
    info.getType() == ConnectivityManager.TYPE_WIFI
     
    二、判断是否是手机网络
    info !=null && info.getType() ==  ConnectivityManager.TYPE_MOBILE
     
    手机网络进行详细区分:
     
    info.getSubtype() 这里使用 getSubtype(),不是 getType(),getType()返回的
    是0,或者1,是区分是手机网络还是wifi
     
    info.getSubtype()取值列表如下:
     
             * NETWORK_TYPE_CDMA 网络类型为CDMA
             * NETWORK_TYPE_EDGE 网络类型为EDGE
             * NETWORK_TYPE_EVDO_0 网络类型为EVDO0
             * NETWORK_TYPE_EVDO_A 网络类型为EVDOA
             * NETWORK_TYPE_GPRS 网络类型为GPRS
             * NETWORK_TYPE_HSDPA 网络类型为HSDPA
             * NETWORK_TYPE_HSPA 网络类型为HSPA
             * NETWORK_TYPE_HSUPA 网络类型为HSUPA
             * NETWORK_TYPE_UMTS 网络类型为UMTS
     
    联通的3G为UMTS或HSDPA,移动和联通的2G为GPRS或EDGE,电信的2G为CDMA,电信
    的3G为EVDO
     
    android获取手机的ip地址
     
    private String getPhoneIp() {
            try {  
                for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
                    NetworkInterface intf = en.nextElement();  
                    for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
                        InetAddress inetAddress = enumIpAddr.nextElement();  
                        if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {  
                        //if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet6Address) {  
                            return inetAddress.getHostAddress().toString();  
                        }  
                    }  
                }  
            } catch (Exception e) {  
            }  
            return ""; 
        }
    

      
    http://www.cnblogs.com/lee0oo0/archive/2013/05/20/3089906.html

  • 相关阅读:
    iOS--------cocoapods遇到的问题
    NTFS
    交换机配置telnet
    交换机
    华为模拟器配置telnet
    路由器
    OSI模型
    网络拓扑
    为什么CAS加锁是线程安全的?--理解原子操作
    零基础自学编程选哪种语言好?世上最好编程语言推荐
  • 原文地址:https://www.cnblogs.com/jiahuafu/p/7081442.html
Copyright © 2011-2022 走看看