Android 获取 IP 地址

java代码
01 public String getLocalIpAddress() {
02 try {
03 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
04 en.hasMoreElements();) {
05 NetworkInterface intf = en.nextElement();
06 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses();
07 enumIpAddr.hasMoreElements();) {
08 InetAddress inetAddress = enumIpAddr.nextElement();
09 if (!inetAddress.isLoopbackAddress()) {
10 return inetAddress.getHostAddress().toString();
11 }
12 }
13 }
14 } catch (SocketException ex) {
15 Log.e(LOG_TAG, ex.toString());
16 }
17 return null;
18 }
posted @ 2012-04-24 23:35  周文  阅读(229)  评论(0编辑  收藏  举报