android 网络接入方式判断

来源:互联网 发布:如何成为淘宝买手 编辑:程序博客网 时间:2024/04/29 23:12
判断网络类型是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 ""; 
    }

判断是否联网:
android 中查看当前是否联网 
方法如下: 
ConnectivityManager cManager=(ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo info = cwjManager.getActiveNetworkInfo(); 
  if (info != null && info.isAvailable()){ 
       //do something 
       //能联网 
        return true; 
  }else{ 
       //do something 
       //不能联网 
        return false; 
  } 
如果为True则表示当前Android手机已经联网,可能是WiFi或GPRS、HSDPA等等,具体的可以通过 ConnectivityManager 类的getActiveNetworkInfo() 方法判断详细的接入方式。 

同时要在manifest里面加个权限 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

文档如下: 
boolean android.net.NetworkInfo.isAvailable() 

public boolean isAvailable () 
Indicates whether network connectivity is possible. A network is unavailable when a persistent or semi-persistent condition prevents the possibility of connecting to that network. Examples include 

The device is out of the coverage area for any network of this type. 
The device is on a network other than the home network (i.e., roaming), and data roaming has been disabled. 
The device's radio is turned off, e.g., because airplane mode is enabled. 

Returns 
true if the network is available, false otherwise

0 0
原创粉丝点击