달력

5

« 2024/5 »

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
728x90
반응형
링크 : http://blog.naver.com/q1q3q5?Redirect=Log&logNo=10099522139

IP 주소를 가져와야 하는 경우가 생길 경우 아래의 함수를 이용하면 됩니다.
jdk1.5버전과 안드로이드 SDK버전 8에서 테스트해봤습니다. InetAddress의 경우 API 1에서부터 지원하였기 때문에, 안드로이드에서는 아래 함수로 IP주소를 가져올 수 있을 것이라고 생각됩니다.


public String getLocalIpAddress() {
    try {
        Enumeration<NetworkInterface> en =
        NetworkInterface.getNetworkInterfaces(); 
        while(en.hasMoreElements()) {
            NetworkInterface interf = en.nextElement();
            Enumeration<InetAddress> ips = interf.getInetAddresses();
            while (ips.hasMoreElements()) {
                InetAddress inetAddress = ips.nextElement();
                if (!inetAddress.isLoopbackAddress()) {
                    return inetAddress.getHostAddress();
                }
            }
        }
    } catch (SocketException ex) {
        Log.e("Testing", ex.toString());
    }
    return null;

 

728x90
반응형
:
Posted by mapagilove