Android 如何获取安卓模拟器的IP地址?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1720346/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 03:31:09  来源:igfitidea点击:

How to get the Android Emulator's IP address?

androidipavd

提问by Rajapandian

I want to get the currently running Android Emulator's IP address through code. How can it be achieved?

我想通过代码获取当前运行的Android Emulator的 IP 地址。如何实现?

回答by Matthias

Just to clarify: from within your app, you can simply refer to the emulator as 'localhost' or 127.0.0.1.

澄清一下:在您的应用程序中,您可以简单地将模拟器称为“localhost”或 127.0.0.1。

Web traffic is routed through your development machine, so the emulator's external IP is whatever IP has been assigned to that machine by your provider. The development machine can always be reached from your device at 10.0.2.2.

Web 流量通过您的开发机器路由,因此模拟器的外部 IP 是您的提供商分配给该机器的任何 IP。始终可以从您的设备在 10.0.2.2 访问开发机器。

Since you were asking only about the emulator's IP, what is it you're trying to do?

由于您只询问仿真器的 IP,您想做什么?

回答by Derek Shockey

If you do truly want the IP assigned to your emulator:

如果您确实希望将 IP 分配给您的模拟器:

adb shell
ifconfig eth0

Which will give you something like:

这会给你类似的东西:

eth0: ip 10.0.2.15 mask 255.255.255.0 flags [up broadcast running multicast]

回答by Marcin Gil

Like this:

像这样:

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;
}

Check the docs for more info: NetworkInterface.

查看文档以获取更多信息:NetworkInterface

回答by Nikhil Agrawal

Use this method you will be getting 100% correct ip address for your android emulator

使用此方法,您将获得 100% 正确的 android 模拟器 IP 地址

To get the ip address of yoor emulator

获取你模拟器的ip地址

Go to adb shell and type this command

转到 adb shell 并键入此命令

adb shell
ifconfig eth0

enter image description here

在此处输入图片说明

After running this command I am getting

运行此命令后,我得到

IP : 10.0.2.15

IP : 10.0.2.15

Mask : 255.255.255.0

掩码:255.255.255.0

Which works for me . I am also working for an networking application.

这对我有用。我也在为一个网络应用程序工作。

回答by mahbub_siddique

If you need to refer to your host computer's localhost, such as when you want the emulator client to contact a server running on the same host, use the alias 10.0.2.2to refer to the host computer's loopback interface. From the emulator's perspective, localhost (127.0.0.1)refers to its own loopback interface.More details: http://developer.android.com/guide/faq/commontasks.html#localhostalias

如果您需要引用主机的 localhost,例如当您希望仿真器客户端联系运行在同一主机上的服务器时,请使用别名10.0.2.2来引用主机的环回接口。从模拟器的角度来看,localhost(127.0.0.1)指的是它自己的环回接口。更多细节:http: //developer.android.com/guide/faq/commontasks.html#localhostalias

回答by den

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;
}

回答by Blasanka

Within the code of my app I can get the running device IP andress easily like beolow:

在我的应用程序代码中,我可以像下面一样轻松获取正在运行的设备 IP 地址:

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());