开发者

How to get the Android Emulator's IP address?

I want to get the curre开发者_JS百科ntly running Android Emulator's IP address through code. How can it be achieved?


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

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

If you have multiple Emulators launched, where adb does not work, unless you pick one by Emulator's Local-IP (like adb -s 192.168.232.2:5555 shell), then:

  • Just like a real Android device,
  • In Emulator, swipe down from top-most, to open menu,
  • In the menu, press and hold on WiFi,
  • Finally, go to settings of current WiFi-connection,
  • There you should see IP of Emulator process.


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

adb shell
ifconfig eth0

Which may give you something like:

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

Or, if above fails, try:

adb shell
ifconfig wlan0

And look for inet addr: in the output, for example, my Emulator logs inet addr:192.168.232.2


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.


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

To get the ip address of yoor emulator

Go to adb shell and type this command

adb shell
ifconfig eth0

How to get the Android Emulator's IP address?

After running this command I am getting

IP : 10.0.2.15

Mask : 255.255.255.0

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


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.2 to 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


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


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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜