Get IP Address

阅读更多
1, Connect via WIFI
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();

2, Connect via GPRS
public String getLocalIpAddress()
{
    try
    {
        for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();)
        {
           NetworkInterface intf = en.nextElement();
           for (Enumeration enumIpAddr =
intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
           {
               InetAddress inetAddress = enumIpAddr.nextElement();
               if (!inetAddress.isLoopbackAddress())
               {
                   return inetAddress.getHostAddress().toString
();
               }
           }
       }
    }
    catch (SocketException ex)
    {
        Log.e(S.TAG, ex.toString());
    }
    return null;
}

你可能感兴趣的:(Get IP Address)