首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

网上这段 获取Android ip地址的代码 没注释 看不懂 哪位高手能给小弟我解释下?多谢

2012-03-20 
网上这段 获取Android ip地址的代码 没注释看不懂 谁能给我解释下?谢谢public static String getLocalIpAd

网上这段 获取Android ip地址的代码 没注释 看不懂 谁能给我解释下?谢谢
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) 
  { 
  Log.e("WifiPreference IpAddress", ex.toString()); 
  } 
  return null; 
  }

[解决办法]
枚举网络接口,如果找到的不是回环(Loopback,127.0.0.1),就返回地址

热点排行