0
的IP我用函數得到IP地址,如下圖所示:Android的 - 獲得一個無線路由器
public static final String IPV4_REGEX = "\\A(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)(\\.(25[0-5]|2[0-4]\\d|[0-1]?\\d?\\d)){3}\\z";
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()&&(inetAddress.getHostAddress().toString().matches(IPV4_REGEX))) {
return /*inetAddress.getHostName()+"~|~"+*/inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException ex) {
ex.printStackTrace();
}
return null;
}
但是我注意到它獲取第一appropriate
IP地址。它工作正常,直到我插入SIM卡。之後,它獲得手機的IP,而不是本地WiFi!所以我想知道如何排除這種情況?我只想得到WiFi路由器的本地IP地址。非常感謝。
是它的工作原理與圈養? – Nolesh 2012-07-11 12:17:24
它排除了IPv6嗎? – Nolesh 2012-07-11 12:20:01
這只是返回wifi的IP地址 – 2012-07-11 12:30:11