Java 获取Android设备上的所有本地IP地址并过滤掉GPRS IP for(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){ NetworkInterface intf=en.nextElement(); 对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){ InetAddress InetAddress=enumIpAddr.nextElement(); 如果(!inetAddress.isLoopbackAddress()&&!inetAddress.isLinkLocalAddress()){ System.out.println(inetAddress.getHostAddress()); } } }

Java 获取Android设备上的所有本地IP地址并过滤掉GPRS IP for(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){ NetworkInterface intf=en.nextElement(); 对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){ InetAddress InetAddress=enumIpAddr.nextElement(); 如果(!inetAddress.isLoopbackAddress()&&!inetAddress.isLinkLocalAddress()){ System.out.println(inetAddress.getHostAddress()); } } },java,android,network-programming,Java,Android,Network Programming,这将返回所有本地IP地址。如果我连接到GPRS,它会返回另一个专用A类IP地址,我想将其过滤掉(因为我不知道这个网络是什么,也不知道它是如何工作的) 我基本上需要WiFi/WiFi热点/USB热点/WiFi直接IP地址 此GPRS接口是否仅连接到一个设备(我的) 可能您可以使用NetworkInterface.getName()来识别它?我可以为我的设备筛选出它,但这不是一个通用的解决方案,对吗?可能您必须与hmmm结合使用我会看一看。接口名为rmnet0,我假设这是一个GPRS接口。这难道不意

这将返回所有本地IP地址。如果我连接到GPRS,它会返回另一个专用A类IP地址,我想将其过滤掉(因为我不知道这个网络是什么,也不知道它是如何工作的)

我基本上需要WiFi/WiFi热点/USB热点/WiFi直接IP地址


此GPRS接口是否仅连接到一个设备(我的)

可能您可以使用NetworkInterface.getName()来识别它?我可以为我的设备筛选出它,但这不是一个通用的解决方案,对吗?可能您必须与hmmm结合使用我会看一看。接口名为rmnet0,我假设这是一个GPRS接口。这难道不意味着我的手机将是唯一连接到它的设备吗?我正在使用这些本地IP进行网络发现,使用UDP广播找出我连接到的网络上的其他设备。因此,如果我的手机是网络中唯一的设备,那么我不会介意:)
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()) {
System.out.println(inetAddress.getHostAddress());
}
}
}