Android(安卓)获取局域网内网IP地址
16lz
2021-01-26
Android 获取局域网内网IP地址
- 废话
- 上代码
- 完事
废话
拿局域网IP一般用在tcp通讯或udp通讯上,下次再整理下这类通讯的框架
上代码
/** * 网络工具 * 获得局域网IP地址 * @author dlong * created at 2019/3/8 2:46 PM */public class NetworkUtils { /** * 获取内网IP地址 * @return * @throws SocketException */ public static String getLocalIPAddress() throws SocketException { 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 instanceof Inet4Address)){ return inetAddress.getHostAddress().toString(); } } } return "null"; }}
kotlin版
import java.net.Inet4Addressimport java.net.InetAddressimport java.net.NetworkInterfaceimport java.util.*/** * @author D10NG * @date on 2019-12-10 14:37 */object NetUtils { /** * 获取内网IP地址 */ val localIPAddress: String get() { val en: Enumeration<NetworkInterface> = NetworkInterface.getNetworkInterfaces() while (en.hasMoreElements()) { val intf: NetworkInterface = en.nextElement() val enumIpAddr: Enumeration<InetAddress> = intf.inetAddresses while (enumIpAddr.hasMoreElements()) { val inetAddress: InetAddress = enumIpAddr.nextElement() if (!inetAddress.isLoopbackAddress && inetAddress is Inet4Address) { return inetAddress.hostAddress.toString() } } } return "null" }}
完事
更多相关文章
- android webrtc 视频流源码获取帧图像VideoFrame转bitmap 作为图
- Android(安卓)TextView显示html富文本格式以及Edittext获取带htm
- JS 判断浏览器客户端类型(ipad,iphone,android)
- Android中获取资源的id和url方法总结
- Android通过URL获取网络数据
- android有用代码片段(一)
- Android获取应用程序的版本名称和版本号
- Android之FileProvider :通过FileProvider来获取content uri
- 【Android】数据存储之Sharepreference