2 baidu 37299572 baidu_37299572 于 2017.06.15 09:00 提问

怎么判断无线网络 IP地址静态还是动态

android怎么在代码里面知道当前设备连接的WIFI 是动态连接还是静态链接?

1个回答

oyljerry
oyljerry   Ds   Rxr 2017.06.15 09:59
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>


 public String getIpAssignment(){

    String myenumvalue="";
    WifiConfiguration wifiConf = null;
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    WifiInfo connectionInfo = wifiManager.getConnectionInfo();
    List<WifiConfiguration> configuredNetworks = wifiManager.getConfiguredNetworks();        
    for (WifiConfiguration conf : configuredNetworks){
        if (conf.networkId == connectionInfo.getNetworkId()){
            wifiConf = conf;
            break;              
        }
    }
    if(wifiConf != null){

        try {
            Object Enumer = getDeclaredField(wifiConf, "ipAssignment");
            myenumvalue = Enumer.toString();
        } catch (SecurityException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }   


    }

    return myenumvalue;
}

查看返回值 UNASSIGNED | STATIC | DHCP

baidu_37299572
baidu_37299572 回复ZWate: 找到方法了!谢谢大神!~
10 天之前 回复
baidu_37299572
baidu_37299572 想请教一下getDeclaredField方法怎么是红色的,如何调用?
10 天之前 回复
Csdn user default icon
上传中...
上传图片
插入图片