当前位置: 首页 > news >正文

android13 系统默认设置静态IP

android11系统的时候,默认静态IP设置很简单,修改frameworks\base\core\res\res\values\config.xml中的config_ethernet_interfaces字符数组,在里面添加静态IP的参数就可以了。
 

    <string-array translatable="false" name="config_ethernet_interfaces"><!--<item>eth1;12,13,14,15;ip=192.168.0.10/24 gateway=192.168.0.1 dns=4.4.4.4,8.8.8.8</item><item>eth2;;ip=192.168.0.11/24</item><item>eth3;12,13,14,15;ip=192.168.0.12/24;1</item>--></string-array>

android13以后,这个参数移动到了packages\modules\Connectivity\service\ServiceConnectivityResources\res\values\config.xml下面,修改以后以太网是能够默认静态IP,但是发现以太网上不了网了,设置动态IP也不行。很是奇怪。后面只能通过另外一种方式去设置,即开机启动的时候去设置一次静态IP。

具体使用如下几个函数:

	public void initEthIp(Context context){boolean issetIP = SystemProperties.getInt("persist.sys.eth_static_ip_flag", 0) == 1 ? true:false;if(issetIP){return;}if(setEthIPAddress(context, "192.168.1.2", "255.255.255.0", "192.168.1.200", "192.168.1.200","8.8.8.8")){SystemProperties.set("persist.sys.eth_static_ip_flag", "1");Log.d(TAG, "set static eth ip ok!");}else{Log.d(TAG, "set static eth ip failed!");}}public boolean setEthIPAddress(Context context, String IP, String Mask, String Gateway, String DNS, String DNS2){String Interface = "eth0";EthernetManager mEthManager;mEthManager = (EthernetManager) (EthernetManager)context.getSystemService(Context.ETHERNET_SERVICE);IpConfiguration.IpAssignment mIpAssignment = IpConfiguration.IpAssignment.DHCP;if (mEthManager != null) {if (isIpAddress(IP) && isIpAddress(Mask) && isIpAddress(Gateway)&& isIpAddress(DNS)) {StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration();if (validateIpConfigFields(mStaticIpConfiguration, IP, Mask,Gateway, DNS, DNS2) != 0) {return false;} else {mEthManager.setEthernetEnabled(false);Inet4Address inetAddr = getIPv4Address(IP);int prefixLength = maskStr2InetMask(Mask);InetAddress gatewayAddr = getIPv4Address(Gateway);InetAddress dnsAddr = getIPv4Address(DNS);if (inetAddr.getAddress().toString().isEmpty() || prefixLength == 0 || gatewayAddr.toString().isEmpty()|| dnsAddr.toString().isEmpty()) {return false;}ArrayList<InetAddress> dnsServers = new ArrayList<>();dnsServers.add(dnsAddr);if (!DNS2.isEmpty()) {dnsServers.add(getIPv4Address(DNS2));}final StaticIpConfiguration.Builder staticIPBuilder = new StaticIpConfiguration.Builder().setDnsServers(dnsServers).setDomains(mStaticIpConfiguration.getDomains()).setGateway(gatewayAddr).setIpAddress(new LinkAddress(inetAddr, prefixLength));mStaticIpConfiguration = staticIPBuilder.build();IpConfiguration ipConfiguration = new IpConfiguration();ipConfiguration.setIpAssignment(IpConfiguration.IpAssignment.STATIC);ipConfiguration.setProxySettings(IpConfiguration.ProxySettings.NONE);ipConfiguration.setStaticIpConfiguration(mStaticIpConfiguration);mEthManager.setConfiguration(Interface, ipConfiguration);mEthManager.setEthernetEnabled(true);return true;}} else {return false;}}return false;}private Inet4Address getIPv4Address(String text) {try {return (Inet4Address) InetAddresses.parseNumericAddress(text);} catch (IllegalArgumentException | ClassCastException e) {return null;}}public int maskStr2InetMask(String maskStr) {StringBuffer sb;String str;int inetmask = 0;int count = 0;/** check the subMask format*/Pattern pattern = Pattern.compile("(^((\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(\\d|[01]?\\d\\d|2[0-4]\\d|25[0-5])$)|^(\\d|[1-2]\\d|3[0-2])$");if (pattern.matcher(maskStr).matches() == false) {Log.e(TAG, "subMask is error");return 0;}String[] ipSegment = maskStr.split("\\.");for (int n = 0; n < ipSegment.length; n++) {sb = new StringBuffer(Integer.toBinaryString(Integer.parseInt(ipSegment[n])));str = sb.reverse().toString();count = 0;for (int i = 0; i < str.length(); i++) {i = str.indexOf("1", i);if (i == -1)break;count++;}inetmask += count;}return inetmask;}private boolean isIpAddress(String value) {int start = 0;int end = value.indexOf('.');int numBlocks = 0;while (start < value.length()) {if (end == -1) {end = value.length();}try {int block = Integer.parseInt(value.substring(start, end));if ((block > 255) || (block < 0)) {return false;}} catch (NumberFormatException e) {return false;}numBlocks++;start = end + 1;end = value.indexOf('.', start);}return numBlocks == 4;}private int validateIpConfigFields(StaticIpConfiguration staticIpConfiguration, String ipAddr,String netmask, String gateway, String dns, String dns2) {final StaticIpConfiguration.Builder staticIPBuilder = new StaticIpConfiguration.Builder().setDnsServers(staticIpConfiguration.getDnsServers()).setDomains(staticIpConfiguration.getDomains()).setGateway(staticIpConfiguration.getGateway()).setIpAddress(staticIpConfiguration.getIpAddress());Inet4Address inetAddr = getIPv4Address(ipAddr);if (inetAddr == null) {return 2;}int networkPrefixLength = -1;networkPrefixLength = maskStr2InetMask(netmask);if(networkPrefixLength == 0){return 3;}InetAddress gatewayAddr = getIPv4Address(gateway);if (gatewayAddr == null) {return 4;}staticIPBuilder.setGateway(gatewayAddr);ArrayList<InetAddress> dnsServers = new ArrayList<>();InetAddress dnsAddr = null;dnsAddr = getIPv4Address(dns);if (dnsAddr == null) {return 5;}dnsServers.add(dnsAddr);InetAddress dnsAddr2 = null;dnsAddr2 = getIPv4Address(dns2);if (dnsAddr2 == null) {return 6;}dnsServers.add(dnsAddr2);staticIpConfiguration  = staticIPBuilder.build();return 0;}

每次开机的时候可以在系统应用中接收开机广播。然后调用initEthIp函数,去检测是否已经设置过静态IP,如果设置过了就用一个系统属性保存标志位,下次再开机的时候判断标志位是否再设置静态IP。


http://www.mrgr.cn/news/36000.html

相关文章:

  • Java——异常处理
  • scala的练习题
  • SOLIDWORKS代理商鑫辰信息科技
  • GitLab 中文发行版最新版重点功能解读
  • ★ 算法OJ题 ★ 前缀和算法(下)
  • 湾区聚力 开源启智 | 2024 CCF中国开源大会暨第五届OpenI/O启智开发者大会闪耀深圳
  • 二叉搜索树(来学包会) C++经验+1
  • GEE 案例:一种在不受云层影响并利用合成口径雷达(SAR)数据的情况下监测植被的方法(双极化SAR植被指数)
  • 【Python】的语言基础学习方法 快速掌握! 源码可分享!
  • 怎么批量制作文本或链接静态码?批量静态码在线的生成技巧
  • 【Webpack】Hash 码
  • 【环境踩坑系列】centos7安装python3.10.X
  • Mybatis-动态SQL
  • Skyeye 云这几年的经历
  • three.js BufferAttribute
  • 计算机网络(九) —— Tcp协议详解
  • python-比较月亮大小/数组下标/人见人爱a+b
  • Webpack 5的新特性:Asset Modules与Dynamic Import
  • Linux快速安装ClickHouse(附官方文档)
  • 【QT 5 调试软件+Linux下调用脚本shell-无法调度+目录拼写+无法找目录+sudo权限(2)+问题解决方式+后续补充】
  • Java中的位图和布隆过滤器(如果想知道Java中有关位图和布隆过滤器的知识点,那么只看这一篇就足够了!)
  • android11 自动授权访问sdcard
  • ChatGPT 向更多用户推出高级语音模式:支持 50 种语言;字节发布两款新视频生成大模型丨 RTE 开发者日报
  • 多城联动、多形式开展网安周公益活动,开源网安传播网络安全知识
  • 解锁视频生成新时代! 探索智谱CogVideoX-2b:轻松生成6秒视频的详细指南
  • 自适应查询优化(Adaptive Query Optimization, AQO)技术简介