2012-02-16 136 views
1

我们正在开发聊天应用程序,我们想要获取使用代码连接到专用网络的仿真器的IP地址。我们正在eclipse中开发代码。android模拟器ip地址

回答

2
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) 
); 

this thread。不要忘了在您的AndroidManifest中拥有INTERNET权限!

希望这会有所帮助!

+0

我们试过这个。但是我们得到了10.0.2.15.This不是我们在模拟器中设置的那个。我们已经设置了192.168.xx.xx.请帮助。预先感谢 – Tejaswini 2012-02-16 16:13:17

+0

我认为这需要ACCESS_WIFI_STATE权限。 – guness 2012-03-30 16:18:53

2

尝试这个代码 -

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

看看这个link

+0

我们试过这个,但是我们得到10.0.2.15.这不是我们在模拟器中设置的那个。我们已经设置了192.168.xx.xx.请帮助。预先感谢 – Tejaswini 2012-02-16 07:21:10