如何判断android设备的IP地址是否公开
当手机设备连接3G网络时,如何判断android设备的ip地址是否是公共的?
public static InetAddress getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress address = enumIpAddr.nextElement();
return address;
}
}
} catch (SocketException ex) {
Log.e(DEBUG_TAG, ex.toString());
}
return null;
}
When phone device connects on 3G network, How can I determine How to determine whether android device's ip address is public or not?
public static InetAddress getLocalIpAddress() {
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress address = enumIpAddr.nextElement();
return address;
}
}
} catch (SocketException ex) {
Log.e(DEBUG_TAG, ex.toString());
}
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编写一个像这样的快速小网页:
然后使用异步任务获取它:
像这样调用它:
然后比较两者。通过上述方法获得的ip保证为您提供公网ip地址。
我知道这是一个老问题,但我希望它对某人有所帮助!
Write a quick little webpage like this:
Then fetch that with an async task:
Call it like:
Then compare the two. The ip you get via the method above is guaranteed to give you the public ip address.
I know this is an old question but I hope it helps somebody!