[b]注意:下面这个方法不能在UI线程运行[/b]
private String getIP() {
String ipstr = "";
try {
Enumeration<NetworkInterface> allNetInterfaces = NetworkInterface.getNetworkInterfaces();
InetAddress ip = null;
while (allNetInterfaces.hasMoreElements())
{
NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
.nextElement();
Enumeration<InetAddress> addresses = netInterface.getInetAddresses();
while (addresses.hasMoreElements()) {
ip = (InetAddress) addresses.nextElement();
if (ip != null && ip instanceof Inet4Address) {
String s = ip.getHostAddress();
if (!s.equals("127.0.0.1")) {
ipstr = s;
break;
}
}
}
if(ipstr.length()>0){
break;
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return ipstr;
}