Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何查找android设备的LAN ip地址?_Android - Fatal编程技术网

如何查找android设备的LAN ip地址?

如何查找android设备的LAN ip地址?,android,Android,如果设备的wifi已连接,我假设该设备具有一个LAN IP地址,该地址可能是由路由器上运行的dhcp分配的 如何找到wifi接口上的LAN ip地址(不是外部ip) 谢谢,将帮助您: String ipAddress = null; try { for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { Net

如果设备的wifi已连接,我假设该设备具有一个LAN IP地址,该地址可能是由路由器上运行的dhcp分配的

如何找到wifi接口上的LAN ip地址(不是外部ip)

谢谢,

将帮助您:

String ipAddress = null;
try {
    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()) {
                ipAddress = inetAddress.getHostAddress().toString();
            }
        }
    }
} catch (SocketException ex) {}
String ipAddress=null;
试一试{
对于(枚举en=NetworkInterface.getNetworkInterfaces();en.hasMoreElements();){
NetworkInterface intf=en.nextElement();
对于(枚举Enumeration EnumipAddress=intf.getInetAddresses();EnumipAddress.hasMoreElements();){
InetAddress InetAddress=enumIpAddr.nextElement();
如果(!inetAddress.isLoopbackAddress()){
ipAddress=inetAddress.getHostAddress().toString();
}
}
}
}catch(SocketException ex){}

“如果设备的wifi已连接,我假设设备具有一个LAN IP地址,该地址可能是由路由器上运行的dhcp分配的。”-不一定。虽然ariefbayu的答案将帮助您找到各种网络接口的IP地址,但您不能假定wifi接口的IP地址是来自LAN的IP地址。各个国家都有“广域”wifi服务-这意味着如果您的要求是与实际的wifi LAN通信,那么如果用户的设备连接到“广域”wifi网络,您的软件可能无法工作。如果连接到3G/4G,这不也会返回WAN地址吗?据我所知,根据当前连接的内容,您将只有一个IP。如果使用WIFI,您将获得本地局域网。如果使用3G/4G,WAN就是你得到的。如果启用(wifi)栓系,则应该有两个地址。你认为呢?当android处于栓系模式时,wifi模块不是会被禁用吗?通过nexus,我可以将WAN连接栓系到wifi连接(手机也是接入点)。在这种情况下,我从未检查过网络api返回的内容。我也从来没有检查过当手机是双卡时会发生什么。我不确定我们是如何在应用程序中模仿Androids topbar的连接状态的