安卓9.0設置以太網靜態IP地址(搬磚)

在做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 "";
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章