当前位置:操作系统 > 安卓/Android >>

android获取本机IP地址

 

Java代码 

/**

     * 获取ip地址

     * 

     * @return

     */ 

    public static String getLocalIpAddress() { 

 

        try { 

 

            for (Enumeration<NetworkInterface> en = NetworkInterface 

                    .getNetworkInterfaces(); 

 

            en.hasMoreElements();) { 

 

                NetworkInterface intf = en.nextElement(); 

 

                for (Enumeration<InetAddress> enumIpAddr = intf 

                        .getInetAddresses(); 

 

                enumIpAddr.hasMoreElements();) { 

 

                    InetAddress inetAddress = enumIpAddr.nextElement(); 

 

                    if (!inetAddress.isLoopbackAddress()) { 

 

                        return inetAddress.getHostAddress().toString(); 

 

                    } 

 

                } 

 

            } 

 

        } catch (SocketException ex) { 

 

        } 

 

        return "127.0.0.1"; 

 

    }   

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,