美文网首页Android开发Android学习Android进阶之路
EthernetManager Android设置以太网静态IP

EthernetManager Android设置以太网静态IP

作者: lebronzhen | 来源:发表于2020-11-13 17:16 被阅读0次

    想要设置以太网为静态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");
        //实例化StaticIpConfiguration
        Object staticIpConfiguration = null;
    
        staticIpConfiguration = staticIpConfigurationCls.newInstance();
        Field ipAddress = staticIpConfigurationCls.getField("ipAddress");
        Field gateway = staticIpConfigurationCls.getField("gateway");
        Field dnsServers = staticIpConfigurationCls.getField("dnsServers");
        //设置ipAddress
        ipAddress.set(staticIpConfiguration, (LinkAddress) cons.newInstance(x));
        //设置网关
        gateway.set(staticIpConfiguration, gatewayAddr);
        //设置dns
        ArrayList<InetAddress> dnsList = (ArrayList<InetAddress>) 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();
        //设置StaticIpConfiguration
        Field staticIpConfigurationField = ipConfigurationCls.getField("staticIpConfiguration");
        staticIpConfigurationField.set(ipConfiguration, staticIpConfiguration);
        //获取ipAssignment、proxySettings的枚举值
        Map<String, Object> ipConfigurationEnum = getIpConfigurationEnum(ipConfigurationCls);
        //设置ipAssignment
        Field ipAssignment = ipConfigurationCls.getField("ipAssignment");
        ipAssignment.set(ipConfiguration, ipConfigurationEnum.get("IpAssignment.STATIC"));
        //设置proxySettings
        Field proxySettings = ipConfigurationCls.getField("proxySettings");
        proxySettings.set(ipConfiguration, ipConfigurationEnum.get("ProxySettings.NONE"));
    
        @SuppressLint("PrivateApi")
        Method setConfigurationMethod = ethernetManagerCls.getDeclaredMethod("setConfiguration", ipConfiguration.getClass());
        //设置静态IP
        setConfigurationMethod.invoke(ethManager, ipConfiguration);
    } catch (NoSuchFieldException | IllegalAccessException | InstantiationException | InvocationTargetException | ClassNotFoundException | NoSuchMethodException e) {
        e.printStackTrace();
    }
    

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

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

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

    相关文章

      网友评论

        本文标题:EthernetManager Android设置以太网静态IP

        本文链接:https://www.haomeiwen.com/subject/whcbbktx.html