主要获取IP地址代码:
/**
 * Get Ip address 自动获取IP地址
 *
 * @throws SocketException
 */
public static String getIpAddress(String ipType) {
    String hostIp = null;
    try {
        Enumeration nis = NetworkInterface.getNetworkInterfaces();
        InetAddress ia = null;
        while (nis.hasMoreElements()) {
            NetworkInterface ni = (NetworkInterface) nis.nextElement();

            Log.e("tiwolf", "getIpAddress: 开机获取ip="+ni.getName() );
            if (ni.getName().equals(ipType)) {


                Enumeration<InetAddress> ias = ni.getInetAddresses();
                while (ias.hasMoreElements()) {

                    ia = ias.nextElement();
                    if (ia instanceof Inet6Address) {
                        continue;// skip ipv6
                    }
                    String ip = ia.getHostAddress();

                    // 过滤掉127段的ip地址
                    if (!"127.0.0.1".equals(ip)) {
                        hostIp = ia.getHostAddress();
                        break;
                    }
                }
            }
        }
    } catch (SocketException e) {
        e.printStackTrace();
    }
    Log.d("tiwolf", "手机IP地址get the IpAddress--> " + hostIp + "");
    return hostIp;
}

调用方法:

 如图:需要哪个IP,就调用哪个iptype

例子:如果我需要eth1的IP地址,那么调用为

String ipStr=IPUtil.getIpAddress("eth1");

更多推荐

Android获取IP地址