Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.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设备中查找以太网连接的子网掩码、网关、DNS值?_Android_Dns_Ethernet_Gateway_Subnet - Fatal编程技术网

如何在android设备中查找以太网连接的子网掩码、网关、DNS值?

如何在android设备中查找以太网连接的子网掩码、网关、DNS值?,android,dns,ethernet,gateway,subnet,Android,Dns,Ethernet,Gateway,Subnet,我有,都是通过以太网连接的。现在,我可以获得设备的IP地址和mac地址,但我还需要获得子网掩码、网关、优先级-秒DNS值。请告诉我如何以编程方式查找这些值 查找Ip地址和mac地址的代码是- connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); networkInfo = connectivityManager.getActiveN

我有,都是通过以太网连接的。现在,我可以获得设备的IP地址和mac地址,但我还需要获得子网掩码、网关、优先级-秒DNS值。请告诉我如何以编程方式查找这些值

查找Ip地址和mac地址的代码是-

  connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        networkInfo = connectivityManager.getActiveNetworkInfo();
        networkType = networkInfo != null ? networkInfo.getTypeName() : "";
  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();
                        System.out.println("ipaddress=" + ipAddress + " formatter ip" + Formatter.formatIpAddress(inetAddress.hashCode()));
                    }
                }
            }
        } catch (SocketException ex) {

        }

   try {
        String interfaceName = "wlan0";
        if (networkType.equals("ETHERNET")) {
            interfaceName = "eth0";
        }
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (!intf.getName().equalsIgnoreCase(interfaceName)) {
                continue;
            }
            byte[] mac = intf.getHardwareAddress();
            if (mac == null) {
                continue;
            }
            StringBuilder buf = new StringBuilder();
            for (byte aMac : mac) {
                buf.append(String.format("%02X:", aMac));
            }
            if (buf.length() > 0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            macaddress = buf.toString();
        }
    } catch (Exception ex) {
        ex.printStackTrace();
    }
connectivityManager=(connectivityManager)context.getSystemService(context.CONNECTIVITY_服务);
networkInfo=connectivityManager.getActiveNetworkInfo();
网络类型=网络信息!=无效的networkInfo.getTypeName():“”;
试一试{
对于(枚举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();
System.out.println(“ipaddress=“+ipaddress+”格式化程序ip“+formatter.FormattIPAddress(inetAddress.hashCode()));
}
}
}
}捕获(SocketException例外){
}
试一试{
字符串interfaceName=“wlan0”;
if(networkType.equals(“以太网”)){
interfaceName=“eth0”;
}
List interfaces=Collections.List(NetworkInterface.getNetworkInterfaces());
用于(网络接口intf:接口){
如果(!intf.getName().equalsIgnoreCase(interfaceName)){
继续;
}
字节[]mac=intf.getHardwareAddress();
如果(mac==null){
继续;
}
StringBuilder buf=新的StringBuilder();
for(字节aMac:mac){
追加(String.format(“%02X:”,aMac));
}
如果(buf.length()>0){
buf.deleteCharAt(buf.length()-1);
}
macaddress=buf.toString();
}
}捕获(例外情况除外){
例如printStackTrace();
}
下面的2个问题可能和你们无关,但我已经记在心里了,所以请任何人解释一下,我会很感激的

1) 如果我同时具有连接WIFI和以太网连接,那么如何识别我的应用程序从哪个连接连接到服务器(除了我使用的网络类型代码之外)


2) WiFi和以太网是否都可以连接到设备中,并且我可以从两种网络类型中获取mac地址、IP地址和所有其他值?

我已经从WiFi网络获取子网和网关,在我的问题中,我从以太网询问。您找到了解决方案吗?
private static String intToIP(int ipAddress) {
String ret = String.format("%d.%d.%d.%d", (ipAddress & 0xff),
  (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff),
  (ipAddress >> 24 & 0xff));
   return ret;
}

public  String GetSubnetMask_WIFI() {
   //  wifii= (WifiManager) 
  getCurrentActivity().getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
  WifiInfo wifiInfo = wifiManager.getConnectionInfo();

  DhcpInfo dhcp = wifiManager.getDhcpInfo();
  String mask = intToIP(dhcp.netmask);

  return mask;
}