`
zengyan2012
  • 浏览: 409834 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

获取IP地址 ----wifi 3G通用

阅读更多

 

----转自:http://blog.csdn.net/android_tutor/article/details/5576544

 

public 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) {

        Log.e(LOG_TAG, ex.toString());

    }

    return null;

}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics