// 获取出来的是包含mac地址和ip的值
public String getEthernetIpAddress() {
final Network network = getFirstEthernet();
if (network == null) {
return null;
}
final StringBuilder sb = new StringBuilder();
boolean gotAddress = false;
final LinkProperties linkProperties = connectivityManager.getLinkProperties(network);
for (LinkAddress linkAddress : linkProperties.getLinkAddresses()) {
sb.append(linkAddress.getAddress().getHostAddress());
gotAddress = true;
}
if (gotAddress) {
return sb.toString();
} else {
return null;
}
}
//使用正则表达式提取出来ip地址
public static String extractIPFromString(String input) {
// 匹配IPv4地址的正则表达式
String ipPattern =
"(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)";
Pattern pattern = Pattern.compile(ipPattern);
Matcher matcher = pattern.matcher(input);
if (matcher.find()) {
return matcher.group();
}
return null;
}
标签:return,String,有线,ip,matcher,android,null,network
From: https://blog.csdn.net/qq_44203816/article/details/137002686