---
title: "Get IP Address"
author: "yinter"
date: "2010-01-14T10:43:57Z"
tags: []
categories: ["移动开发"]
slug: "get-ip-address"
draft: false
lastmod: "2010-01-14T10:43:57Z"
id: 569899
url: "https://yinter.iteye.com/blog/569899"
---

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<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(S.TAG, ex.toString());
    }
    return null;
}