android获取ip(webtrc-app)

Show your ip by webrtc

代码: 链接

实际演示效果: demo链接

参考: https://github.com/diafygi/webrtc-ips

Show your ip in app

使用java对应的接口查询目前的ip,包括wifi的ip和移动数据网络的ip,不一定每次能够查到所有的ip,与系统是否开启wifi、是否开启移动网络等相关。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public static List<String> getIPAddress() {

ArrayList<String> iplist = new ArrayList<String>();
try {
//Enumeration<NetworkInterface> en=NetworkInterface.getNetworkInterfaces();
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
InetAddress inetAddress = enumIpAddr.nextElement();
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
Log.d(MainActivity.class.getName(), "ip is: " + inetAddress.getHostAddress());
iplist.add(inetAddress.getHostAddress());
}
}
}
} catch (SocketException e) {
e.printStackTrace();
}

return iplist;
}

参考: https://www.cnblogs.com/anni-qianqian/p/8084656.html