Tejaswini
Tejaswini

Reputation: 357

android emulator ip address

We are developing chat application and we want to fetch the IP address of the emulator which is connected to private network using the code.We are developing code in eclipse.

Upvotes: 1

Views: 1273

Answers (2)

Nick Badal
Nick Badal

Reputation: 671

WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
ipString = String.format( 
    "%d.%d.%d.%d", 
    (ip & 0xff), 
    (ip >> 8 & 0xff),
    (ip >> 16 & 0xff),
    (ip >> 24 & 0xff)
);

From this thread. Don't forget to have INTERNET permission in your AndroidManifest!

Hope this helps!

Upvotes: 2

Galaxy S2
Galaxy S2

Reputation: 438

Try out this code -

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 out this link

Upvotes: 2

Related Questions