Android開發(fā)準確獲取手機IP地址的兩種方式
最近看了好多網(wǎng)上獲取IP地址的例子,發(fā)現(xiàn)好多都不完全準確,這里我寫一下獲取ip地址的兩種方式。
比如微信支付,后臺在做接口的時候,要求App端傳入IP地址,我們需要判斷是網(wǎng)絡(luò)環(huán)境,WI-FI還是3G,所以需要獲取這兩種環(huán)境的ip地址。
第一步:首先是判斷網(wǎng)絡(luò)環(huán)境:
String ip; ConnectivityManager conMann = (ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo mobileNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_MOBILE); NetworkInfo wifiNetworkInfo = conMann.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (mobileNetworkInfo.isConnected()) { ip = getLocalIpAddress(); System.out.println("本地ip-----"+ip); }else if(wifiNetworkInfo.isConnected()) { WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); ip = intToIp(ipAddress); System.out.println("wifi_ip地址為------"+ip); }
如果連接的是移動網(wǎng)絡(luò),第二步,獲取本地ip地址:getLocalIpAddress();這樣獲取的是ipv4格式的ip地址。
public String getLocalIpAddress() { try { String ipv4; ArrayList<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface ni: nilist) { ArrayList<InetAddress> ialist = Collections.list(ni.getInetAddresses()); for (InetAddress address: ialist){ if (!address.isLoopbackAddress() && InetAddressUtils.isIPv4Address(ipv4=address.getHostAddress())) { return ipv4; } } } } catch (SocketException ex) { Log.e("localip", ex.toString()); } return null; }
如果連接的是WI-FI網(wǎng)絡(luò),第三步,獲取WI-FI ip地址:intToIp(ipAddress);
public static String intToIp(int ipInt) { StringBuilder sb = new StringBuilder(); sb.append(ipInt & 0xFF).append("."); sb.append((ipInt >> 8) & 0xFF).append("."); sb.append((ipInt >> 16) & 0xFF).append("."); sb.append((ipInt >> 24) & 0xFF); return sb.toString(); }
網(wǎng)上的很多代碼獲取的是ipv6的本地ip,在微信支付里這種ip地址無法調(diào)起微信支付,附代碼:
private String getlocalIp() { String ip; 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()&&!inetAddress.isLinkLocalAddress()) { // ip=inetAddress.getHostAddress().toString(); System.out.println("ip=========="+inetAddress.getHostAddress().toString()); return inetAddress.getHostAddress().toString(); } } } } catch (SocketException ex) { Log.e("WifiPreference IpAddress", ex.toString()); } return null; }
本文主要介紹了Android準確獲取手機IP地址的兩種方式,更多關(guān)于Android獲取手機IP地址的方式請查看下面的相關(guān)鏈接
相關(guān)文章
一文搞懂Android RecyclerView點擊展開、折疊效果的實現(xiàn)代碼
雖然在日常開發(fā)中已經(jīng)多次接觸過RecycleView,但也只是用到其最基本的功能,并沒有深入研究其他內(nèi)容。接下來將抽出時間去了解RecycleView的相關(guān)內(nèi)容,這篇文章主要是介紹Android RecyclerView點擊展開、折疊效果的實現(xiàn)方式,一起看看吧2021-06-06Android通過overScrollBy實現(xiàn)下拉視差特效
這篇文章主要為大家詳細介紹了Android通過overScrollBy實現(xiàn)下拉視差特效,實現(xiàn)精彩的阻尼效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-08-08Android繼承ViewGroup實現(xiàn)Scroll滑動效果的方法示例
這篇文章主要介紹了Android繼承ViewGroup實現(xiàn)Scroll滑動效果的方法,結(jié)合實例形式分析了Android滑動效果的原理及擴展ViewGroup實現(xiàn)滑動功能的相關(guān)操作技巧,需要的朋友可以參考下2017-08-08Android編程實現(xiàn)ListView滾動提示等待框功能示例
這篇文章主要介紹了Android編程實現(xiàn)ListView滾動提示等待框功能,結(jié)合實例形式分析了Android ListView滾動事件相關(guān)實現(xiàn)技巧,需要的朋友可以參考下2017-02-02