未连接 Wi-Fi 时的内部 IP 地址
Internal IP address when not on Wi-Fi
我在连接到 Wi-Fi 时使用它来获取内部 IP:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipv4intAddress = wifiInfo.getIpAddress();
tempView = (TextView) findViewById(R.id.textViewIPv4Int2);
tempView.setText(myNetworkOperations.iptoDecimal(ipv4intAddress));
在移动设备或其他类型的连接上是否有类似的简单方法来获取该 IP?
您可以尝试使用此代码:
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) {
return "ERROR Obtaining IP";
}
return "No IP Available";
我在连接到 Wi-Fi 时使用它来获取内部 IP:
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipv4intAddress = wifiInfo.getIpAddress();
tempView = (TextView) findViewById(R.id.textViewIPv4Int2);
tempView.setText(myNetworkOperations.iptoDecimal(ipv4intAddress));
在移动设备或其他类型的连接上是否有类似的简单方法来获取该 IP?
您可以尝试使用此代码:
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) {
return "ERROR Obtaining IP";
}
return "No IP Available";