EthernetManager Android设置以太网静态IP

想要设置以太网为静态IP通过搜索是需用到EthernetManager,但是EthernetManager是谷歌隐藏的API,app是无法调用到的,所以只能通过反射来进行设置

也可以通过下载系统的class.jar包,放到自己的项目中,就可以不用反射,直接调用

下边是引入jar包直接调用的代码,通过EthernetManager 的setConfiguration方法来设置,但是需要构造IpConfiguration 和StaticIpConfiguration对象,IpConfiguration.IpAssignment.STATIC就代表设置为静态IP,也可以设置DHCP

private void setStaticIP() {EthernetManager mEthManager = (EthernetManager) getSystemService("ethernet");StaticIpConfiguration mStaticIpConfiguration = new StaticIpConfiguration();/** get ip address, netmask,dns ,gw etc.*/Inet4Address inetAddr = getIPv4Address(mEthIpAddress);int prefixLength = NetUtils.maskStr2InetMask(mEthNetmask);InetAddress gatewayAddr = getIPv4Address(mEthGateway);InetAddress dnsAddr = getIPv4Address(mEthdns1);if (inetAddr.getAddress().toString().isEmpty() || prefixLength ==0 || gatewayAddr.toString().isEmpty()|| dnsAddr.toString().isEmpty()) {return;}Class clazz = null;try {clazz = Class.forName("android.net.LinkAddress");} catch (Exception e) {// TODO: handle exception}Class[] cl = new Class[]{InetAddress.class, int.class};Constructor cons = null;//取得所有构造函数try {cons = clazz.getConstructor(cl);} catch (NoSuchMethodException e) {e.printStackTrace();}//给传入参数赋初值Object[] x = {inetAddr, prefixLength};String dnsStr2 = mEthdns2;//mStaticIpConfiguration.ipAddress = new LinkAddress(inetAddr, prefixLength);try {mStaticIpConfiguration.ipAddress = (LinkAddress) cons.newInstance(x);} catch (InstantiationException e) {e.printStackTrace();} catch (IllegalAccessException e) {e.printStackTrace();} catch (InvocationTargetException e) {e.printStackTrace();}mStaticIpConfiguration.gateway=gatewayAddr;mStaticIpConfiguration.dnsServers.add(dnsAddr);if (!dnsStr2.isEmpty()) {mStaticIpConfiguration.dnsServers.add(getIPv4Address(dnsStr2));}Log.d("2312321", "chanson mStaticIpConfiguration  ====" + mStaticIpConfiguration);IpConfiguration mIpConfiguration = new IpConfiguration(IpConfiguration.IpAssignment.STATIC, IpConfiguration.ProxySettings.NONE, mStaticIpConfiguration, null);mEthManager.setConfiguration(mIpConfiguration);}

参考项目链接

不想引用jar包的话就需要使用反射来实现,下边代码是我模仿这个逻辑通过反射来实现的代码

try {Class ethernetManagerCls = Class.forName("android.net.EthernetManager");//获取EthernetManager实例Object ethManager = VfiServiceApp.getContext().getSystemService("ethernet");// get ip address, netmask,dns ,gw etc.Inet4Address inetAddr = getIPv4Address(STATIC_IP);int prefixLength = NetUtils.maskStr2InetMask(STATIC_NETMASK);InetAddress gatewayAddr = getIPv4Address(STATIC_GATEWAY);InetAddress dnsAddr = getIPv4Address(STATIC_DNS1);Class clazz = Class.forName("android.net.LinkAddress");if (ethManager == null || inetAddr == null || inetAddr.getAddress() == null || gatewayAddr == null|| dnsAddr == null || Arrays.toString(inetAddr.getAddress()).isEmpty() || prefixLength == 0|| gatewayAddr.toString().isEmpty() || dnsAddr.toString().isEmpty() || clazz == null) {return;}Class[] cl = new Class[]{InetAddress.class, int.class};Constructor cons = null;//取得所有构造函数try {cons = clazz.getConstructor(cl);} catch (NoSuchMethodException e) {e.printStackTrace();}if (cons == null) {return;}//给传入参数赋初值Object[] x = {inetAddr, prefixLength};Class staticIpConfigurationCls = Class.forName("android.net.StaticIpConfiguration");//实例化StaticIpConfigurationObject staticIpConfiguration = null;staticIpConfiguration = staticIpConfigurationCls.newInstance();Field ipAddress = staticIpConfigurationCls.getField("ipAddress");Field gateway = staticIpConfigurationCls.getField("gateway");Field dnsServers = staticIpConfigurationCls.getField("dnsServers");//设置ipAddressipAddress.set(staticIpConfiguration, (LinkAddress) cons.newInstance(x));//设置网关gateway.set(staticIpConfiguration, gatewayAddr);//设置dnsArrayList dnsList = (ArrayList) dnsServers.get(staticIpConfiguration);dnsList.add(dnsAddr);if (!STATIC_DNS2.isEmpty()) {dnsList.add(getIPv4Address(STATIC_DNS2));}Log.d(TAG, "chanson mStaticIpConfiguration  ====" + staticIpConfigurationCls);Class ipConfigurationCls = Class.forName("android.net.IpConfiguration");Object ipConfiguration = ipConfigurationCls.newInstance();//设置StaticIpConfigurationField staticIpConfigurationField = ipConfigurationCls.getField("staticIpConfiguration");staticIpConfigurationField.set(ipConfiguration, staticIpConfiguration);//获取ipAssignment、proxySettings的枚举值Map ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls);//设置ipAssignmentField ipAssignment = ipConfigurationCls.getField("ipAssignment");ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.STATIC"));//设置proxySettingsField proxySettings = ipConfigurationCls.getField("proxySettings");proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.NONE"));@SuppressLint("PrivateApi")Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass());//设置静态IPsetConfigurationMethod.invoke(ethManager, ipConfiguration);
} catch (NoSuchFieldException | IllegalAccessException | InstantiationException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException e) {e.printStackTrace();
}

最好放到主线程中执行,因为我之前测试在子线程有报错,所以需要切换到主线程,参考文章https://www.jianshu.com/p/16792c2ef112

两种方式都可以设置成功,根据个人需要使用即可,class.jar可以通过到参考项目libs中进行下载

当然还需要应用有系统权限才可以设置,所以需要改为系统应用并进行系统签名才可真正设置成功



作者:lebronzhen
链接:https://www.jianshu.com/p/d8ef3699d534
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部