如何找到互联网连接类型是否通过java代码Wifi或LAN连接不与android

问题描述 投票:0回答:2

公共类ConnetionType {

public static void main(String[] args) {NetworkInterface ni=new NetworkInterface();

}

}

java network-programming
2个回答
0
投票

public class networktest {

public static void main(String[] args) {
    try{
    Enumeration<NetworkInterface>eni=NetworkInterface.getNetworkInterfaces();
    while(eni.hasMoreElements()){
        NetworkInterface nii=eni.nextElement();
        if(nii.isUp())
                System.out.println("you are connected to:"+nii.getDisplayName());
        }
    }catch(Exception e){
        e.printStackTrace();
    }
}

}


0
投票

如果您在Windows计算机上运行,​​则可以使用命令行:

try {
   Process p = Runtime.getRuntime().exec("netsh interface show interface");
   String line;
   java.io.BufferedReader input = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
   int counter = 0;
   while ((line = input.readLine()) != null) {
      counter++;
      if (counter > 3) { // The first lines are headlines, so it can be ignored
         System.out.println(line);
      }
   }
   input.close();
} catch (Exception e) {
   e.printStackTrace();
}

有关该命令的说明,请参阅here

© www.soinside.com 2019 - 2024. All rights reserved.