안드로이드 - 3G / Wifi 상황에서 IP 가져오기 안드로이드 이야기2012. 8. 29. 00:50
안드로이드 - 3G / Wifi 상황에서 IP 가져오기
출처 : http://blog.naver.com/ziippy/120138070384
public String getLocalIpAddress()
{
final String IP_NONE = "N/A";
final String WIFI_DEVICE_PREFIX = "eth";
String LocalIP = IP_NONE;
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()) {
if( LocalIP.equals(IP_NONE) )
LocalIP = inetAddress.getHostAddress().toString();
else if( intf.getName().startsWith(WIFI_DEVICE_PREFIX) )
LocalIP = inetAddress.getHostAddress().toString();
}
}
}
} catch (SocketException e) {
Log.e("getLocalIpAddress()", "getLocalIpAddress Exception:"+e.toString());
}
return LocalIP;
}
'안드로이드 이야기' 카테고리의 다른 글
안드로이드 - 서버/클라이언트 소켓(Socket) 통신하기 (0) | 2012.08.29 |
---|---|
안드로이드 - 소켓을 이용한 카메라 연결 (0) | 2012.08.29 |
안드로이드 - IP 주소 가져오기 (0) | 2012.08.29 |
안드로이드 - ip 가져오기 (1) | 2012.08.29 |
안드로이드 - 이미지 파일 서버로 업로드하기 (0) | 2012.08.28 |