静态IP与动态IP

实现一个小功能就是点击CheckButton时实现静态IP与动态IP的切换:

相关类在以下目录:

frameworks\base\core\java\android\net

  1. public void changceIP(boolean autoIp){ Log.i(TAG, "changceIp"+autoIp); EthernetStatus.setEnabled(autoIp); if(autoIp){ //动态IP获取,是网络分派的 mIpAssignment = IpAssignment.DHCP;//设置成动态的 mProxySettings = ProxySettings.NONE; mIpconfiguration.setStaticIpConfiguration(null); mIpconfiguration = mEthernetManager.getConfiguration(); mIpconfiguration.setIpAssignment(IpAssignment.DHCP);//设置成动态的 mIpconfiguration.setProxySettings(ProxySettings.NONE); mEthernetManager.setConfiguration(mIpconfiguration); }else{ //自己手动设置IP mIpconfiguration = mEthernetManager.getConfiguration(); mIpconfiguration.setIpAssignment(IpAssignment.STATIC);//设置成静态的 mIpconfiguration.setProxySettings(ProxySettings.NONE); mIpAssignment = IpAssignment.STATIC; mProxySettings = ProxySettings.NONE; StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration(); String gateway = mEthernetHolder.getEthernetV4Gateway(); String Dns1 = mEthernetHolder.getEthernetV4Dns1();//这些就是通过ExitText用户输入IP 来获取String类型的IP String Dns2 = mEthernetHolder.getEthernetV4Dns2(); String ipAddr = mEthernetHolder.getEthernetV4Address(); if(!TextUtils.isEmpty(ipAddr)){ try{ //然后把String类型IP转换成相应的IP类型,这是IP InetAddress inetAddr = (InetAddress) NetworkUtils.numericToInetAddress(ipAddr); LinkAddress linkAddress = new LinkAddress(inetAddr,24,0,0); mStaticIpConfiguration.ipAddress = linkAddress; }catch(IllegalArgumentException|ClassCastException e){ } } InetAddress inetGateway = null; if (!TextUtils.isEmpty(gateway)) { try { //Log.i(TAG, "GATEWAY="+gateway);这是网关 inetGateway =(InetAddress)NetworkUtils.numericToInetAddress(gateway); //Settings.System.putString(context.getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY, mRoutes); //RouteInfo routeInfo = new RouteInfo(inetGateway); //linkProperties.addRoute(routeInfo); //RouteInfo routeInfo = new RouteInfo(inetGateway); //linkProperties.addRoute(routeInfo); mStaticIpConfiguration.gateway = inetGateway; } catch (IllegalArgumentException|ClassCastException e) { } } InetAddress dns1inetA = null; if (!TextUtils.isEmpty(Dns1)) { try { //这是域名系统1 Log.i(TAG, "dns1="+Dns1); dns1inetA = (InetAddress) NetworkUtils.numericToInetAddress(Dns1); mStaticIpConfiguration.dnsServers.add(dns1inetA ); // linkProperties.addDnsServer(dns1inetA); } catch (IllegalArgumentException|ClassCastException e) { } } InetAddress dns2inetA = null; if (!TextUtils.isEmpty(Dns2)) { try { Log.i(TAG, "dns2="+Dns2); //域名系统2 dns2inetA = (InetAddress) NetworkUtils.numericToInetAddress(Dns2); mStaticIpConfiguration.dnsServers.add(dns2inetA ); } catch (IllegalArgumentException|ClassCastException e) { } //然后把静态IP的对象往IpConfiguration中设置 mIpconfiguration.setStaticIpConfiguration(mStaticIpConfiguration); //最后再把IpConfiguration对象往有线管理类中设置 mEthernetManager.setConfiguration(mIpconfiguration); } } }

转载于:https://www.cnblogs.com/alanz/p/5362355.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值