android中怎样获取IP地址

WIFI和G3卡,都获取了争取的Ip地址:代码如下:

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

你可能感兴趣的:(android,IP)