本篇文章的内容,来自网络资源“Android高手进阶教程”,不是自己写的。欢迎大家看看这本教程,写的不错。
/**
*
* getLocalIpAddress(获取手机IP地址的方法)
* @param name
* @return String DOM对象
* @Exception 异常对象
* @since CodingExample Ver(编码范例查看) 1.1
*/
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()) {//获取本机IP地址
return inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
Log.e("TEST", ex.toString());
}
return null;
}