在Android上获得活动的运行网络接口
我想知道我的Android设备上的哪个网络接口是当前活动的,特别是我想知道哪个接口当前正在“运行”,而不仅仅是“启动” 为了实现这一点,我试图通过读取位于以下位置的每个接口的“标志”文件来读取每个网络接口的标志集:在Android上获得活动的运行网络接口,android,Android,我想知道我的Android设备上的哪个网络接口是当前活动的,特别是我想知道哪个接口当前正在“运行”,而不仅仅是“启动” 为了实现这一点,我试图通过读取位于以下位置的每个接口的“标志”文件来读取每个网络接口的标志集: /sys/class/net/*interface*/flags 使用OS monitor,我看到为wlan0设置了以下标志: 读取/sys/class/net/wlan0/flags的内容,我得到以下结果: 0x1003 => 0000 0000 0001 0000 00
/sys/class/net/*interface*/flags
使用OS monitor,我看到为wlan0设置了以下标志:
读取/sys/class/net/wlan0/flags的内容,我得到以下结果:
0x1003 => 0000 0000 0001 0000 0000 0011
根据,为了匹配OS Monitor提供的信息,应如下设置标志:
0000 0000 0001 0000 0100 0011 => 0x1043
换言之:缺少运行标志!:(
为什么不是所有的旗子都设置好了
现在我确实相信OS Monitor使用JNI和IOCTL以某种方式获取它们的信息,但我希望避免使用JNI
你们中有谁能帮我弄清楚如何为每个接口获取此类信息吗
PS:运行ipconfig wlan0也会显示标志“Running”。好的,所以我找到了另一种方法 我只需使用下面的代码遍历所有文件,用它们的文件名调用ifconfig并解析结果
public static String getRunningInterfaceName(){
File folder = new File("/sys/class/net/");
File[] files = folder.listFiles();
for(int i = 0; i < files.length; i++){
// Open all "flags" files in subdirs, and ignore tun interfaces.
if(files[i].isDirectory() && !files[i].getName().contains("tun")){
try {
String[] commands = {"ifconfig", files[i].getName()};
Process ifconfig = Runtime.getRuntime().exec(commands);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(ifconfig.getInputStream()));
ifconfig.waitFor();
String s = null;
while ((s = stdInput.readLine()) != null) {
if(!s.contains("loopback") && s.contains("running")){
stdInput.close();
return files[i].getName();
}
}
stdInput.close();
} catch (IOException ioe){
ioe.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
return null;
}
公共静态字符串getRunningInterfaceName(){
文件夹=新文件(“/sys/class/net/”);
File[]files=文件夹.listFiles();
对于(int i=0;i