通用获取Ip的方法

1106阅读 0评论2010-11-11 Leanx
分类:嵌入式

  1. public String getLocalIpAddress() {  
  2.     try {  
  3.         for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {  
  4.             NetworkInterface intf = en.nextElement();  
  5.             for (Enumeration enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {  
  6.                 InetAddress inetAddress = enumIpAddr.nextElement();  
  7.                 if (!inetAddress.isLoopbackAddress()) {  
  8.                     return inetAddress.getHostAddress().toString();  
  9.                 }  
  10.             }  
  11.         }  
  12.     } catch (SocketException ex) {  
  13.         Log.e(LOG_TAG, ex.toString());  
  14.     }  
  15.     return null;  

上一篇:动态增加TextView
下一篇:面试题1