一直以來,好像沒有一段標準的代碼能提供Android設備此刻的IP地址,究其原因,Android設備的網卡可能不只一個,如蜂窩網卡、WiFi網卡,而且同一個網卡也可能擁有不止一個IP地址。基于此,一個Android終端很有可能同時擁有多個IP地址(不只是同時擁有IPv4和IPv6地址),比如開啟熱點共享蜂窩網絡的時候,蜂窩網卡擁有一個IPv4地址來訪問外網,WiFi網卡擁有一個IPv4地址來作為內網的網關。
網上比較流行的獲取Android設備IP地址的代碼有以下幾種,下面我們來一一分析一下。
1. 不可行的方法
String ipAddress = Inet4Address.getLocalHost().getHostAddress()
這個是Java提供的API,在Android上執行需要以下權限(經測試Android版本6.0.1的一部機器不需要該權限,比較納悶,求解答)
<uses-permission android:name="android.permission.INTERNET"/>
此外,由于該方法使用了網絡通信,因此不能在UI線程執行。
該方法顧名思義是獲取本地主機的IP地址,在某些Java平臺上可以得到想要的結果,但是我截取了Android官方給出的關于該方法的部分說明如下:
Returns an InetAddress for the local host if possible, or the loopback address otherwise. This method works by getting the hostname, performing a DNS lookup, and then taking the first returned address.
Note that if the host doesn't have a hostname set – as Android devices typically don't – this method will effectively return the loopback address, albeit by getting the name localhost and then doing a lookup to translate that to 127.0.0.1.
可以看出,一般在Android平臺上,由于網絡通信設備沒有設置hostname,因此無法進行DNS檢索得到其相應的IP地址,因此該方法會返回本地回環地址,即127.0.0.1,也就是說這個方法在Android平臺上無法達到我們一般的獲取本機IP地址的目的,經過測試,結果也確實如此。
2. 部分可行的方法
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
int ipAddressInt = wm.getConnectionInfo().getIpAddress();
String ipAddress = String.format(Locale.getDefault(), "%d.%d.%d.%d", (ipAddressInt & 0xff), (ipAddressInt >> 8 & 0xff), (ipAddressInt >> 16 & 0xff), (ipAddressInt >> 24 & 0xff));
方法執行所需權限為:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
需要說明的是,上述代碼第二行返回的是一個int類型的值,如1795336384,它對應的十六進制值6b02a8c0每兩位便對應IPv4地址的每一項(逆序,如c0轉化為十進制為192)。
經測試,通過該方法可以獲得當前WiFi網絡中Android設備的IPv4地址,但是顯然,該方法是通過WifiManager獲取當前網絡連接下的IP地址的,因此它只局限于使用WiFi網絡的情況,當使用蜂窩等其他網絡設備時,該方法無效,會返回0值。另外,如果你是通過比較hacker的方式比如沒有通過系統Framework層打開WiFi,而是自己通過Linux命令創建的WiFi網絡,那么像這種Framework層提供的API也是不起作用的。
3. 基本可行的方法
public static String getIpAddressString() {
try {
for (Enumeration<NetworkInterface> enNetI = NetworkInterface
.getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
NetworkInterface netI = enNetI.nextElement();
for (Enumeration<InetAddress> enumIpAddr = netI
.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
return inetAddress.getHostAddress();
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}
return "";
}
方法執行所需權限為:
<uses-permission android:name="android.permission.INTERNET"/>
這段代碼不難理解,其實就是雙重循環獲取終端中所有網絡接口的所有IP地址,然后返回第一個遇到的非本地回環的IPv4地址。這種方式可以很好的覆蓋我們一般的需求。根據Android系統的運行機制,當WiFi網絡開啟時蜂窩網絡會自動關閉,因此遍歷到的第一個地址是WiFi網卡的IP地址;同樣,當關閉WiFi網絡,打開蜂窩網絡時,遍歷到的第一個地址是蜂窩網卡的IP地址。
那么,為什么我叫這種方式為基本可行的方法呢,因為它返回的結果并不是百分百“正確”的,確切地說并不一定是開發人員想要的結果。比如當Android手機開啟熱點的時候,實際上是通過WiFi網卡共享其蜂窩網絡,因此此時,WiFi網卡和蜂窩網卡分配了不同的IP地址,但由于蜂窩網卡對應的NetworkInterface對象出現的位置要先于WiFi網卡,因此該方法返回的實際上是蜂窩網卡的IP地址。如果想要始終獲取WiFi網卡的IP地址可以在上述的兩個循環間添加如下篩選代碼:
if (netI.getDisplayName().equals("wlan0") || netI.getDisplayName().equals("eth0"))
其中"wlan0"和"eth0"為常見的WLAN網卡的DisplayName名稱,絕大部分為"wlan0",比較老的機型可能會是"eth0"或其他。
這里只是舉了一個簡單的例子,其實還有很多特殊的情況,比如開啟USB網絡共享的情況、開啟網絡代理的情況、之前提到的Hacker手段同時打開蜂窩網絡和WiFi網絡(非WiFi熱點)的情況等等,這些網絡環境下都會存在多IP的情況,因此該方法不一定完全適用了。
正如文章開頭所說,由于一個Android設備同一時刻可能不只有一個IP地址,因此可以說沒有任何一段通用的代碼能獲取每個人心中想要獲取的IP地址,重要的還是根據自己具體的需求來進行相應的代碼修改,通過對獲取的IP地址列表進行篩選來得到想要的結果。
本文的討論是圍繞IPv4地址的,如果想要獲取IPv6地址,Android API也提供了相應的類或方法,只需要在上述代碼的基礎上作出微小修改即可。
最后附上在StackOverFlow上看到的關于IP地址篩選的總結,供大家參考。
- Any address in the range 127.xxx.xxx.xxx is a "loopback" address. It is only visible to "this" host.
- Any address in the range 192.168.xxx.xxx is a private (aka site local) IP address. These are reserved for use within an organization. The same applies to 10.xxx.xxx.xxx addresses, and 172.16.xxx.xxx through 172.31.xxx.xxx.
- Addresses in the range 169.254.xxx.xxx are link local IP addresses. These are reserved for use on a single network segment.
- Addresses in the range 224.xxx.xxx.xxx through 239.xxx.xxx.xxx are multicast addresses.
- The address 255.255.255.255 is the broadcast address.
- Anything else should be a valid public point-to-point IPv4 address.
文中所有代碼可以在個人github主頁查看和下載。
另,個人技術博客,同步更新,歡迎關注!轉載請注明出處!文中若有什么錯誤希望大家探討指正!