Android开发--获取本地ip(wifi 3g)

来源:互联网 发布:nginx ip转发 编辑:程序博客网 时间:2024/06/06 21:04
/**     * 获取本地ip     * @return     */     public String getLocalIpAddress()        {            String ipaddress = "";            try            {                Enumeration<NetworkInterface> en = NetworkInterface                        .getNetworkInterfaces();                // 遍历所用的网络接口                while (en.hasMoreElements())                {                    NetworkInterface nif = en.nextElement();// 得到每一                             Enumeration<InetAddress> inet = nif.getInetAddresses();                    // 遍历每一个接口绑定的所有ip                    while (inet.hasMoreElements())                    {                        InetAddress ip = inet.nextElement();                        if (!ip.isLoopbackAddress()                                && InetAddressUtils.isIPv4Address(ip                                        .getHostAddress()))                        {                            return ipaddress =ip.getHostAddress();                        }                    }                }            }            catch (SocketException e)            {                Log.e("feige", "获取本地ip地址失败");                e.printStackTrace();            }            return ipaddress;        }

在做支付的时候用到了。列出来,避免下次再找!

0 0