1--安卓網路程式設計之獲取IP地址
關於IP
1.IP 網際協議地址(Internet Protocol Address)
2.是TCP/IP協議族中網路層的協議
3.網際網路上的每一個網路和每一臺主機分配一個邏輯地址,以此來遮蔽實體地址的差異
IPv4
1.IP協議第四版
2.位數為32位,即32個0,1排列組合形成的不同數值,每個數值可對應Internet的一個邏輯地址,共2的32次方個。
3.每個邏輯地址可供一個裝置連線到Internet,通過這個邏輯地址就能尋找到某個裝置。
獲取手機的IPv4

ipv4安卓測試.png
僅開啟移動資料時: {ccmni0=10.66.253.0, lo=127.0.0.1} 開啟手機熱點時: {ccmni0=10.66.253.0, lo=127.0.0.1, ap0=192.168.43.1} 連上WLAN(無線時): {Mac=1c:77:f6:11:1e:4e, name="FAST_4BB", ip=192.168.10.111, BSSID=d8:15:0d:4b:b7:a0}
可見手機流量走的是:ccmni0名稱的ip
手機熱點會產生一個ip: ap0名稱的ip
都會有一個本地的ip地址:127.0.0.1
程式碼實現:這裡用Properties盛放資訊
判斷網路型別需要的物件: NetworkInfo
物件info
獲取方法:通過Context獲取系統服務 ConnectivityManager
再 getActiveNetworkInfo()
通過 info.getType()
:
行動網路2G/3G/4G: ConnectivityManager.TYPE_MOBILE
WIFI: ConnectivityManager.TYPE_WIFI
行動網路IP:獲取 NetworkInterface
物件
獲取NetworkInterface迭代列舉nis: NetworkInterface.getNetworkInterfaces()
獲取InetAddress迭代列舉ias: ni.getInetAddresses
WIFI:獲取 WifiInfo
物件
獲取方法:通過Context獲取系統服務 WifiManager
再 getConnectionInfo()
/** * 獲取IPv4 * * @param context * @return */ public static Properties getIPAddress(Context context) { //將ip資訊儲存在Properties物件中 Properties prop = new Properties(); //獲取網路資訊 NetworkInfo info = ((ConnectivityManager) context .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo(); //網路資訊非空,並且已連線 if (info != null && info.isConnected()) { //當前使用2G/3G/4G網路 if (info.getType() == ConnectivityManager.TYPE_MOBILE) { try { //獲取NetworkInterface的迭代列舉 Enumeration<NetworkInterface> nis = NetworkInterface.getNetworkInterfaces(); while (nis.hasMoreElements()) {//開始迭代 NetworkInterface ni = nis.nextElement();//獲取每一個NetworkInterface //獲取InetAddress的迭代列舉 Enumeration<InetAddress> ias = ni.getInetAddresses(); while (ias.hasMoreElements()) {//開始迭代 InetAddress ia = ias.nextElement();//獲取每一個InetAddress //只取IPv4 if (ia instanceof Inet4Address) { //將屬性設定到集合 prop.setProperty(ni.getDisplayName(), ia.getHostAddress()); } } } } catch (SocketException e) { e.printStackTrace(); } } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {//當前使用無線網路 //通過WIFI管理者獲取Wifi資訊 WifiInfo wifiInfo = ((WifiManager) context.getSystemService(Context.WIFI_SERVICE)) .getConnectionInfo(); //將屬性設定到集合 prop.setProperty("ip", int2StrIP(wifiInfo.getIpAddress()));//得到IPV4地址 prop.setProperty("name", wifiInfo.getSSID()); prop.setProperty("BSSID", wifiInfo.getBSSID()); prop.setProperty("Mac", wifiInfo.getMacAddress()); } } else { //當前無網路連線 prop.setProperty("ERROR", "請開啟網路"); } return prop; } /** * 將得到的int型別的IP轉換為String型別 * * @param ip * @return */ public static String int2StrIP(int ip) { return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "." + ((ip >> 16) & 0xFF) + "." + (ip >> 24 & 0xFF); }
使用:
Properties prop = IpUtils.getIPAddress(IPActivity.this); Enumeration<?> names = prop.propertyNames(); while (names.hasMoreElements()) { String s = (String) names.nextElement(); log.e("TAG",(s + "=" + prop.getProperty(s)) + "\n"); }

debug檢視安卓網路資訊.png
後記:捷文規範
1.本文成長記錄及勘誤表
專案原始碼 | 日期 | 備註 |
---|---|---|
V0.1--無 | 2018-10-14 | ofollow,noindex">1--安卓網路程式設計之獲取IP地址 |
2.更多關於我
筆名 | 微信 | 愛好 | |
---|---|---|---|
張風捷特烈 | 1981462002 | zdl1994328 | 語言 |
我的github | 我的簡書 | 我的CSDN | 個人網站 |
3.宣告
1----本文由張風捷特烈原創,轉載請註明
2----歡迎廣大程式設計愛好者共同交流
3----個人能力有限,如有不正之處歡迎大家批評指證,必定虛心改正
4----看到這裡,我在此感謝你的喜歡與支援