在做android设备时候有设置设备的静态IP,因为这个方法是系统隐藏方法,而且还是9.0的系统目前测试验证一下博客的方法可行。

原博客:安卓9.0设置以太网静态IP地址
博客来源地址:https://blog.csdn.net/zzhceo/article/details/99596435

以上只是设置没有获取已经设置的信息,故自己参照以上博客,实现了获取已经设置好的静态IP信息。

/**
     * 获取静态信息
     */
    public static Map<String, String> getEthernnetIp(Context context) {
        Map<String, String> ipMaps = new HashMap<>(16);
        try {
            String mInterfaceName = "eth0";
            EthernetManager mEthManager = getEthernetManager(context);
            if (mEthManager == null) {
                return ipMaps;
            }
            String assignMent = mEthManager.getConfiguration(mInterfaceName).ipAssignment.name();
            // if assignMent is dhcp, no need repeat set
            if ("DHCP".equals(assignMent)) {
                Log.d(TAG, " setEthernetIP mode == assignment = DHCP, no need repeat set");
                return ipMaps;
            }
            IpConfiguration configuration = mEthManager.getConfiguration(mInterfaceName);
            if (configuration != null) {
                StaticIpConfiguration staticIpConfiguration = configuration.getStaticIpConfiguration();
                if (staticIpConfiguration != null) {
                    ArrayList<InetAddress> dnsServers = staticIpConfiguration.dnsServers;
                    InetAddress gateway = staticIpConfiguration.gateway;
                    LinkAddress ipAddress = staticIpConfiguration.ipAddress;

                    if (gateway != null) {
                        ipMaps.put("gateway", gateway.getHostAddress());
                    }
                    if (ipAddress != null) {
                        InetAddress address = ipAddress.getAddress();
                        if (address != null) {
                            ipMaps.put("ip", address.getHostAddress());
                        }

//                      int prefixLength = ipAddress.getPrefixLength();
                        String s = ipAddress.toString();
                        String mask = getMask(s);
                        ipMaps.put("mask", mask);
                    }

                    if (dnsServers != null && dnsServers.size() > 1) {
                        ipMaps.put("dns", dnsServers.get(0).getHostAddress());
                    }

                    Log.d(TAG, ipMaps.toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ipMaps;
    }

然后把,反射进行抽取

 private static EthernetManager getEthernetManager(Context context) {
        // get EthernetManager instance by reflect @{
        Object ethernetManagerInstance = null;
        try {
            Class<?> ethernetManagerClass = Class.forName("android.net.EthernetManager");
            Class<?> iEthernetManagerClass = Class.forName("android.net.IEthernetManager");
            // 获取ETHERNET_SERVICE参数
            String ETHERNET_SERVICE = (String) Context.class.getField("ETHERNET_SERVICE").get(null);
            // 获取ethernetManager服务对象
            Object ethernetManager = context.getSystemService(ETHERNET_SERVICE);
            // 获取在EthernetManager中的抽象类mService成员变量
            Field mService = ethernetManagerClass.getDeclaredField("mService");
            // 修改private权限
            mService.setAccessible(true);
            // 获取抽象类的实例化对象
            Object mServiceObject = mService.get(ethernetManager);
            ethernetManagerInstance = ethernetManagerClass
                    .getDeclaredConstructor(Context.class, iEthernetManagerClass).newInstance(context, mServiceObject);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return (EthernetManager) ethernetManagerInstance;
    }

子网掩码反转代码:

 /**
     * 数值子网掩码转换成IP的格式
     */
    public static String getMask(String addr) {
        try {
            String[] parts = addr.split("/");
            int prefix;
            if (parts.length < 2) {
                prefix = 0;
            } else {
                prefix = Integer.parseInt(parts[1]);
            }
            int mask = 0xffffffff << (32 - prefix);

            byte[] bytes = new byte[]{
                    (byte) (mask >>> 24), (byte) (mask >> 16 & 0xff), (byte) (mask >> 8 & 0xff), (byte) (mask & 0xff)};

            InetAddress netAddr = InetAddress.getByAddress(bytes);

            return netAddr.getHostAddress();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }
Logo

智屏生态联盟致力于大屏生态发展,利用大屏快应用技术降低开发者开发、发布大屏应用门槛

更多推荐