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

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.kler.cn/a/325128.html

相关文章:

  • H.265流媒体播放器EasyPlayer.js网页直播/点播播放器WebGL: CONTEXT_LOST_WEBGL错误引发的原因
  • 网页作业9
  • SpringSecurity+jwt+captcha登录认证授权总结
  • Uniapp 引入 Android aar 包 和 Android 离线打包
  • 【linux】如何扩展磁盘容量(VMware虚拟机)-转载
  • IDEA优雅debug
  • VMware下Ubuntu找不到共享文件夹
  • 4. 将pycharm本地项目同步到(Linux)服务器上——深度学习·科研实践·从0到1
  • Latex 自定义运算符加限定条件的实现
  • WPF入门教学十 资源与字典
  • Rust结构体初探
  • linux中实现多路复用服务器
  • 使用Python创建EXE运行器和截图工具
  • 【数据结构和算法实践-排序-总结】
  • 9.24作业
  • Uniapp 打包后的横屏控制
  • 【JavaEE初阶】多线程7(面试要点)
  • MacOS安装MindSpore(2024年最新)
  • 创意实现!在uni-app小程序商品详情页轮播中嵌入视频播放功能
  • 成都睿明智科技有限公司可靠吗?
  • SpringBoot--为什么Controller是串行的?怎样才能并行?
  • uni-app之旅-day01-home页
  • Python 课程18-SQLAlchemy
  • Stable Diffusion绘画 | LCM模型:实现秒出图
  • 多旋翼无人机光伏发电站吊运技术详解
  • nodejs基于vue电子产品商城销售网站的设计与实现 _bugfu