一、对于一些工控行业的Android主板一般都会扩展个以太网接口,也就是可以插网线进行上网;然后在系统设置页面有个以太网菜单可以进行IP、网关等设置,如下:


二、那怎么通过代码去设置呢?
1、Android系统在Framework层是有提供操作以太网Api的也就是android.net.EthernetManager,但是Google把Api给隐藏了;所以最终你是调用不了的

2、那需要怎么操作才能调用到隐藏的Api呢?相信你肯定会立马想到使用反射去调用;对,没错!这里我就是使用的这个方式;当然如果你公司系统层也是自己搞的话就更简单了
三、先假装如果可以调用到android.net.EthernetManager的Api,那么代码应该怎么写呢?
-
设置静态IP地址, new LinkAddress()报错是因为源码@hide了

-
设置动态ip地址

四、接下来就是通反射去调用了,代码量就比上面多的多了
1/** 2 * 文件名: EthernetUtil 3 * 创建时间: 2020/8/21 on 11:22 4 * 描述: TODO 设置以太网静态ip 动态ip地址 5 * 6 * @author 阿钟 7 */ 8 9public class EthernetUtil { 10 11 /** 12 * 设置以太网动态获取IP 13 */ 14 public static boolean setDynamicIp(Context context) { 15 try { 16 Class<?> ethernetManagerCls = Class.forName("android.net.EthernetManager"); 17 //获取EthernetManager实例 18 Object ethManager = context.getSystemService("ethernet"); 19 //创建IpConfiguration 20 Class<?> ipConfigurationCls = Class.forName("android.net.IpConfiguration"); 21 Object ipConfiguration = ipConfigurationCls.newInstance(); 22 //获取ipAssignment、proxySettings的枚举值 23 Map<String, Object> ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls); 24 //设置ipAssignment 25 Field ipAssignment = ipConfigurationCls.getField("ipAssignment"); 26 ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.DHCP")); 27 //设置proxySettings 28 Field proxySettings = ipConfigurationCls.getField("proxySettings"); 29 proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.NONE")); 30 //获取EthernetManager的setConfiguration() 31 Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass()); 32 //设置动态IP 33 setConfigurationMethod.invoke(ethManager, ipConfiguration); 34 return true; 35 } catch (Exception e) { 36 e.printStackTrace(); 37 return false; 38 } 39 } 40 41 /** 42 * 设置以太网静态IP地址 43 * 44 * @param address ip地址 45 * @param mask 子网掩码 46 * @param gate 网关 47 * @param dns dns 48 */ 49 public static boolean setEthernetStaticIp(Context context, String address, String mask, String gate, String dns) { 50 try { 51 Class<?> ethernetManagerCls = Class.forName("android.net.EthernetManager"); 52 //获取EthernetManager实例 53 Object ethManager = context.getSystemService("ethernet"); 54 //创建StaticIpConfiguration 55 Object staticIpConfiguration = newStaticIpConfiguration(address, gate, mask, dns); 56 //创建IpConfiguration 57 Object ipConfiguration = newIpConfiguration(staticIpConfiguration); 58 //获取EthernetManager的setConfiguration() 59 Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass()); 60 //保存静态ip设置 61 saveIpSettings(context, address, mask, gate, dns); 62 //设置静态IP 63 setConfigurationMethod.invoke(ethManager, ipConfiguration); 64 return true; 65 } catch (Exception e) { 66 e.printStackTrace(); 67 return false; 68 } 69 } 70 71 72 /** 73 * 获取StaticIpConfiguration实例 74 */ 75 private static Object newStaticIpConfiguration(String address, String gate, String mask, String dns) throws Exception { 76 Class<?> staticIpConfigurationCls = Class.forName("android.net.StaticIpConfiguration"); 77 //实例化StaticIpConfiguration 78 Object staticIpConfiguration = staticIpConfigurationCls.newInstance(); 79 Field ipAddress = staticIpConfigurationCls.getField("ipAddress"); 80 Field gateway = staticIpConfigurationCls.getField("gateway"); 81 Field domains = staticIpConfigurationCls.getField("domains"); 82 Field dnsServers = staticIpConfigurationCls.getField("dnsServers"); 83 //设置ipAddress 84 ipAddress.set(staticIpConfiguration, newLinkAddress(address, mask)); 85 //设置网关 86 gateway.set(staticIpConfiguration, InetAddress.getByName(gate)); 87 //设置掩码 88 domains.set(staticIpConfiguration, mask); 89 //设置dns 90 ArrayList<InetAddress> dnsList = (ArrayList<InetAddress>) dnsServers.get(staticIpConfiguration); 91 dnsList.add(InetAddress.getByName(dns)); 92 return staticIpConfiguration; 93 } 94 95 /** 96 * 获取LinkAddress实例 97 */ 98 private static Object newLinkAddress(String address, String mask) throws Exception { 99 Class<?> linkAddressCls = Class.forName("android.net.LinkAddress"); 100 Constructor<?> linkAddressConstructor = linkAddressCls.getDeclaredConstructor(InetAddress.class, int.class); 101 return linkAddressConstructor.newInstance(InetAddress.getByName(address), getPrefixLength(mask)); 102 } 103 104 /** 105 * 获取IpConfiguration实例 106 */ 107 private static Object newIpConfiguration(Object staticIpConfiguration) throws Exception { 108 Class<?> ipConfigurationCls = Class.forName("android.net.IpConfiguration"); 109 Object ipConfiguration = ipConfigurationCls.newInstance(); 110 //设置StaticIpConfiguration 111 Field staticIpConfigurationField = ipConfigurationCls.getField("staticIpConfiguration"); 112 staticIpConfigurationField.set(ipConfiguration, staticIpConfiguration); 113 //获取ipAssignment、proxySettings的枚举值 114 Map<String, Object> ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls); 115 //设置ipAssignment 116 Field ipAssignment = ipConfigurationCls.getField("ipAssignment"); 117 ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.STATIC")); 118 //设置proxySettings 119 Field proxySettings = ipConfigurationCls.getField("proxySettings"); 120 proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.STATIC")); 121 return ipConfiguration; 122 } 123 124 /** 125 * 获取IpConfiguration的枚举值 126 */ 127 private static Map<String, Object> getIpConfigurationEnum(Class<?> ipConfigurationCls) { 128 Map<String, Object> enumMap = new HashMap<>(); 129 Class<?>[] enumClass = ipConfigurationCls.getDeclaredClasses(); 130 for (Class<?> enumC : enumClass) { 131 Object[] enumConstants = enumC.getEnumConstants(); 132 if (enumConstants == null) continue; 133 for (Object enu : enumConstants) { 134 enumMap.put(enumC.getSimpleName() + "." + enu.toString(), enu); 135 } 136 } 137 return enumMap; 138 } 139 140 /** 141 * 保存静态ip设置 142 */ 143 private static void saveIpSettings(Context context, String address, String mask, String gate, String dns) { 144 ContentResolver contentResolver = context.getContentResolver(); 145 Settings.Global.putString(contentResolver, "ethernet_static_ip", address); 146 Settings.Global.putString(contentResolver, "ethernet_static_mask", mask); 147 Settings.Global.putString(contentResolver, "ethernet_static_gateway", gate); 148 Settings.Global.putString(contentResolver, "ethernet_static_dns1", dns); 149 } 150 151 /** 152 * 获取长度 153 */ 154 private static int getPrefixLength(String mask) { 155 String[] strs = mask.split("\\."); 156 int count = 0; 157 for (String str : strs) { 158 if (str.equals("255")) { 159 ++count; 160 } 161 } 162 return count * 8; 163 } 164}
-
使用只需调用方法即可:
//设置静态ip EthernetUtil.setEthernetStaticIp(context, "192.168.5.33", "255.255.255.0", "192.168.5.1", "8.8.8.8"); //设置动态获取ip EthernetUtil.setDynamicIp(context)
最后:这个代码本人在Android7.0的原生系统验证是可以使用的,有需要的可以使用试试EthernetUtil源码下载地址
- 最后在推荐个好用的系统源码查看网址吧:Android 系统源码在线查看