获取网络信息,ip,子网掩码,网关,dns

无线网络信息可以通过api直接获取,无需特别处理,下面主要是机顶盒等android设备使用有线网络时网络信息的获取。

1.网络类型

    /**
     * 获取网络类型
     * @return
     *      -1:未知网络
     *       0:无网络
     *       1:wifi网络
     *       2:移动网络
     *       3:以太网
     */
    public static int getNetWorkInfo() {
        ConnectivityManager connectivityManager = (ConnectivityManager) MyApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo info = connectivityManager.getActiveNetworkInfo();
        if (info != null && info.isAvailable()) {
            if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                return 1;
            }
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                return 2;
            }
            if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {
                return 3;
            }
        } else {
            return 0;
        }
        return -1;
    }

2.获取详细网络信息

	static final String etName = "eth0";

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    private static NetWorkInfo getETnetwork(NetWorkInfo netWorkInfo) {
        EthernetManager mEthernetManager = (EthernetManager) MyApplication.getContext().getSystemService("ethernet");
        IpConfiguration configuration = mEthernetManager.getConfiguration();
        if (configuration == null) {
            return netWorkInfo;
        }
        IpConfiguration.IpAssignment ipAssignment = configuration.getIpAssignment();
        if (ipAssignment == null) {
            return netWorkInfo;
        }
        String ip = "";
        String mask = "";
        String gateway = "";
        String dns1 = "";
        String dns2 = "";
        if (IpConfiguration.IpAssignment.DHCP == ipAssignment) {//DHCP自动分配方式
            Log.i(TAG, "DHCP");
            ip = getSystemOsString("dhcp." + etName + ".ipaddress");//ip
            mask = getSystemOsString("dhcp." + etName + ".mask");
            gateway = getSystemOsString("dhcp." + etName + ".gateway");
            dns1 = getSystemOsString("dhcp." + etName + ".dns1");
            dns2 = getSystemOsString("dhcp." + etName + ".dns2");

        } else if (IpConfiguration.IpAssignment.STATIC == ipAssignment) {//手动设置方式
            Log.i(TAG, "STATIC");
            ip = configuration.getStaticIpConfiguration().ipAddress.getAddress().getHostAddress();
            mask = getMask(configuration.getStaticIpConfiguration().ipAddress.getPrefixLength());
            gateway = configuration.getStaticIpConfiguration().gateway.getHostAddress();
            dns1 = configuration.getStaticIpConfiguration().dnsServers.get(0).getHostAddress();
            dns2 = configuration.getStaticIpConfiguration().dnsServers.get(1).getHostAddress();
        }
        netWorkInfo.mask = mask;//子网掩码
        netWorkInfo.gateway = gateway;//网关
        netWorkInfo.dns1 = dns1;//dns1
        netWorkInfo.dns2 = dns2;//dns2
        return netWorkInfo;
    }

    /**
     * 计算子网掩码
     */
    private static String getMask(int prefix) {
        StringBuilder stb = new StringBuilder();
        StringBuilder result = new StringBuilder();
        for (int i = 1; i <= 32; i++) {
            stb.append(i <= prefix ? "1" : "0");
            if (i % 8 == 0) {
                result.append(Integer.parseInt(stb.toString(), 2)).append(".");
                stb.setLength(0);
            }
        }
        return result.deleteCharAt(result.length() - 1).toString();
    }

    private static String getSystemOsString(String property) {
        Class clazz = null;
        try {
            clazz = Class.forName("android.os.SystemProperties");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            return unknown;
        }
        Method met;
        try {
            met = clazz.getMethod("get", String.class, String.class);
            met.setAccessible(true);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
            return unknown;
        }
        Object obj = null;
        try {
            obj = met.invoke(null, new Object[]{property, "unknown"});
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
            return unknown;
        } catch (IllegalAccessException e) {
            e.printStackTrace();
            return unknown;
        } catch (InvocationTargetException e) {
            e.printStackTrace();
            return unknown;
        }
        return (String) obj;
    }

关于子网掩码的计算:
prefixLength最大32位,可以分成4组8个1的二进制,正好是255,255,255,255,少N位就要在末尾去掉N个1加上N个0,然后再把4组二进制数据换成十进制就得出最终结果;
例如:
30 = 255,255,255,252
26 = 255,255,255,192
最后,EthernetManager这个类是被隐藏的,可以自己把这一部分的android源码加到自己的项目里去调用,也可以用我上传的jar包直接用。
android.net.jar

你可能感兴趣的:(Android)