Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 如何獲得手機的IP地址代碼段

android 如何獲得手機的IP地址代碼段

編輯:關於Android編程

[java] 
<SPAN style="COLOR: #993300"> Connected via WIFI 
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE); 
WifiInfo wifiInfo = wifiManager.getConnectionInfo(); 
int ipAddress = wifiInfo.getIpAddress();</SPAN> 

 Connected via WIFI
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();


[java]
<SPAN style="COLOR: #993300"> Connected 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; 
}</SPAN> 

 Connected 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;
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved