int addressType = activeNetInfo.getAddresses().get(0).getAddressFamily();找不到getAddresses()方法
时间: 2024-10-28 17:13:03 浏览: 7
对不起,我犯了一个错误。`getAddresses()`方法是在`LinkAddress[]`数组上而不是直接在`NetworkInfo`对象上,所以应该先获取所有地址列表再访问第一个地址。以下是修正后的代码:
```java
NetworkInfo activeNetInfo = connManager.getActiveNetworkInfo();
if (activeNetInfo != null && (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI || activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE)) {
LinkProperties linkProperties = activeNetInfo.getLinkProperties();
List<inet6_address_t> inetAddresses = linkProperties.getGlobalIPv6AddressList();
if (!inetAddresses.isEmpty()) {
int addressType = inetAddresses.get(0).getAddressFamily();
if (addressType == NetworkInterface.ANYCAST_IPADDR || addressType == InternetProtocol.IPPROTO_IPV6) {
Log.d("App", "Device supports IPv6");
} else {
Log.d("App", "Device does not support IPv6");
}
} else {
Log.d("App", "Device does not have a global IPv6 address");
}
}
```
这里我们先从`linkProperties`中获取全球IPv6地址列表,然后检查列表是否为空以及地址类型是否为IPv6。
阅读全文