问题描述
什么是检查在IPv6可在currient android手机的最佳方式是什么?
What is the best way to check if IPv6 is available on the currient android phone?
我的currient想法是使用 的NetworkInterface
并通过枚举 NetworkInterface.getNetworkInterfaces()
但这似乎是太复杂了。
My currient idea is to use NetworkInterface
and to enumerate via NetworkInterface.getNetworkInterfaces()
but this seems to be too complicated.
有没有一种更简单的方法?
Is there a simpler way?
推荐答案
我不知道一个简单的方法比使用的如果您需要检查所有的接口,但它不应该是不好:
I don't know of a simpler way than using NetworkInterface
if you need to check all of the interfaces, but it shouldn't be that bad:
for(NetworkInterface netInt: NetworkInterface.getNetworkInterfaces()){
for(InterfaceAddress address: netInt.getInterfaceAddresses()){
if(address.getAddress() instancof Inet6Address){
// found IPv6 address
// do any other validation of address you may need here
}
}
}
如果你知道地址要检查你可以跳过使用的NetworkInterface
,并通过调用检查具体的InetAddress
的InetAddress
一个人的静态 getBy ...()
方法,并检查是否是一个实例是Inet6Address
。
if you know the address you want to check you can skip using NetworkInterface
and check the specific InetAddress
by calling one of InetAddress
's static getBy...()
methods and check whether that is an instance of Inet6Address
.
这篇关于最好的方法来检查,如果IPv6的可用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!