如何获取本地网络Ipv4地址

public staticString getLocalIpv4Address(Context context) {

ConnectivityManager cm = (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);

try{

Method e = ConnectivityManager.class.getMethod("getActiveLinkProperties",newClass[0]);

LinkProperties prop = (LinkProperties)e.invoke(cm,newObject[0]);

returnformatIpAddresses(prop);

}catch(NoSuchMethodException var4) {

var4.printStackTrace();

}catch(InvocationTargetException var5) {

var5.printStackTrace();

}catch(IllegalAccessException var6) {

var6.printStackTrace();

}

return"127.0.0.1";

}

private staticString formatIpAddresses(LinkProperties prop) {

if(prop ==null) {

return null;

}else{

try{

Method e = LinkProperties.class.getMethod("getAddresses",newClass[0]);

e.setAccessible(true);

Collection addresses = (Collection)e.invoke(prop,newObject[0]);

Iterator var4 = addresses.iterator();

while(var4.hasNext()) {

InetAddress address = (InetAddress)var4.next();

if(addressinstanceofInet4Address) {

returnaddress.getHostAddress();

}

}

}catch(NoSuchMethodException var5) {

var5.printStackTrace();

}catch(InvocationTargetException var6) {

var6.printStackTrace();

}catch(IllegalAccessException var7) {

var7.printStackTrace();

}

return"127.0.0.1";

}

}

你可能感兴趣的:(如何获取本地网络Ipv4地址)