我想通过代码获取当前运行的Android Emulator的IP地址。如何实现这一目标?
p4tfgftt1#
只是澄清一下:从您的应用程序中,您可以简单地将Emulator称为“localhost”或127.0.0.1。Web流量通过您的开发计算机进行路由,因此Emulator的External-IP是您的Internet提供商分配给该开发计算机的任何External-IP。开发机器始终可以通过10.0.2.2从您的设备访问。如果您启动了多个Emulator,其中adb不工作,除非您通过Emulator的Local-IP(如adb -s 192.168.232.2:5555 shell)pick one,则:
localhost
127.0.0.1
10.0.2.2
adb
adb -s 192.168.232.2:5555 shell
yqlxgs2m2#
如果您确实希望将IP分配给模拟器:
adb shellifconfig eth0
adb shell
ifconfig eth0
这可能会给予你这样的东西:
eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]
或者,如果上面失败,请尝试:
adb shellifconfig wlan0
ifconfig wlan0
并在输出中查找inet addr:,例如,我的Emulator记录了inet addr:192.168.232.2
inet addr:
inet addr:192.168.232.2
gajydyqb3#
像这样:
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()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e(LOG_TAG, ex.toString()); } return null;}
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()) {
return inetAddress.getHostAddress().toString();
}
} catch (SocketException ex) {
Log.e(LOG_TAG, ex.toString());
return null;
查看文档了解更多信息:NetworkInterface。
83qze16e4#
使用此方法,您将获得100%正确的ip地址为您的android模拟器
获取yoor模拟器的ip地址转到adb shell并键入以下命令
运行此命令后,我得到
IP:10.0.2.15掩码:255.255.255.0
这对我很有效。我也在做网络应用。
xxls0lw85#
如果您需要引用主机的localhost,例如当您希望模拟器客户端联系主机上运行的服务器时,请使用别名10.0.2.2来引用主机的环回接口。从模拟器的Angular 来看,**localhost(127.0.0.1)**指的是自己的环回接口。更多详情:https://developer.android.com/studio/run/emulator-networking#networkaddresses
9vw9lbht6#
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()) { return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e(LOG_TAG, ex.toString()); } return null;}
for (Enumeration < NetworkInterface > en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
for (Enumeration < InetAddress > enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
5anewei67#
在我的应用程序的代码中,我可以像beolow一样轻松获取运行设备的IP地址:
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
7条答案
按热度按时间p4tfgftt1#
只是澄清一下:从您的应用程序中,您可以简单地将Emulator称为“
localhost
”或127.0.0.1
。Web流量通过您的开发计算机进行路由,因此Emulator的External-IP是您的Internet提供商分配给该开发计算机的任何External-IP。开发机器始终可以通过
10.0.2.2
从您的设备访问。如果您启动了多个Emulator,其中
adb
不工作,除非您通过Emulator的Local-IP(如adb -s 192.168.232.2:5555 shell
)pick one,则:yqlxgs2m2#
如果您确实希望将IP分配给模拟器:
这可能会给予你这样的东西:
或者,如果上面失败,请尝试:
并在输出中查找
inet addr:
,例如,我的Emulator记录了inet addr:192.168.232.2
gajydyqb3#
像这样:
查看文档了解更多信息:NetworkInterface。
83qze16e4#
使用此方法,您将获得100%正确的ip地址为您的android模拟器
获取yoor模拟器的ip地址
转到adb shell并键入以下命令
运行此命令后,我得到
IP:10.0.2.15
掩码:255.255.255.0
这对我很有效。我也在做网络应用。
xxls0lw85#
如果您需要引用主机的localhost,例如当您希望模拟器客户端联系主机上运行的服务器时,请使用别名10.0.2.2来引用主机的环回接口。从模拟器的Angular 来看,**localhost(127.0.0.1)**指的是自己的环回接口。
更多详情:https://developer.android.com/studio/run/emulator-networking#networkaddresses
9vw9lbht6#
5anewei67#
在我的应用程序的代码中,我可以像beolow一样轻松获取运行设备的IP地址: