android 获取平板的ip,Android平板电脑IP地址

/*查看可用的网络接口并选择第一个“正派”的IPv4地址。 *由于模拟器默认使用10.0.2.15,所以只有在没有更好的可用的情况下才使用它。 */

public String getMyIp() {

Set eligible = eligibleIpAddresses();

/* For the emulator, prefer an IP address other than 10.0.2.15 (default emulator address)

* but use it if it is the only one. */

if (eligible.size() > 1) {

eligible.remove("10.0.2.15");

return eligible.iterator().next();

} else if (eligible.size() == 1) {

return eligible.iterator().next();

} else {

Log.w("Using local IP address, no external objects will be discovered","---");

return "127.0.0.1";

}

}

public static Set eligibleIpAddresses() {

Set eligible = new HashSet();

try {

Enumeration netInterfaces = NetworkInterface.getNetworkInterfaces();

while (netInterfaces.hasMoreElements()) {

NetworkInterface ni = netInterfaces.nextElement();

Enumeration address = ni.getInetAddresses();

while (address.hasMoreElements()) {

InetAddress addr = address.nextElement();

if (!addr.isLoopbackAddress() && !(addr.getHostAddress().indexOf(":") > -1)) {

eligible.add(addr.getHostAddress());

}

}

}

} catch (Exception e) {

}

return eligible;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值