andorid跳过系统API获取以太网IP,网关,DNS,MAC的方法

最近在android平台上构建出windows的效果,之前通过EthernetManager和EthernetDevInfo获取以太网相关信息,但为了边敲代码边运行,想要跳过系统层的api调用,方法如下:

  • 获取DNS:
private String getLocalDNS(){
          Process cmdProcess = null;
          BufferedReader reader = null;
          String dnsIP = "";
          try {
              cmdProcess = Runtime.getRuntime().exec("getprop dhcp.eth0.dns1");
              reader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
              dnsIP = reader.readLine();
              return dnsIP;
          } catch (IOException e) {
              return null;
          } finally{
          try {
             reader.close();
              } catch (IOException e) {
                 }
                cmdProcess.destroy();
              }
    }
  • 获取网关:
private String getLocalGATE(){
        Process cmdProcess = null;
        BufferedReader reader = null;
        String dnsIP = "";
        try {
            cmdProcess = Runtime.getRuntime().exec("getprop dhcp.eth0.gateway");
            reader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
            dnsIP = reader.readLine();
            return dnsIP;
        } catch (IOException e) {
            return null;
        } finally{
            try {
                reader.close();
          } catch (IOException e) {
             }
            cmdProcess.destroy();
          }
    }
  • 获取ip地址:
private String getLocalGATE(){
        Process cmdProcess = null;
        BufferedReader reader = null;
        String dnsIP = "";
        try {
            cmdProcess = Runtime.getRuntime().exec("getprop dhcp.eth0.ipaddress");
            reader = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
            dnsIP = reader.readLine();
            return dnsIP;
        } catch (IOException e) {
            return null;
        } finally{
            try {
                reader.close();
          } catch (IOException e) {
             }
            cmdProcess.destroy();
          }
    }

上面的方法只有使用的指令不一样,追求代码简洁的朋友可以合成一个方法来写,或者写入工具类中。

  • 其他相关指令:
root@android:/ # getprop | grep eth0
getprop | grep eth0
[dhcp.eth0.dns1]: [192.168.0.1]
[dhcp.eth0.dns2]: []
[dhcp.eth0.dns3]: []
[dhcp.eth0.dns4]: []
[dhcp.eth0.gateway]: [192.168.0.1]
[dhcp.eth0.ipaddress]: [192.168.0.180]
[dhcp.eth0.leasetime]: [3600]
[dhcp.eth0.mask]: [255.255.255.0]
[dhcp.eth0.pid]: [13800]
[dhcp.eth0.reason]: [PREINIT]
[dhcp.eth0.result]: [failed]
[dhcp.eth0.server]: [192.168.0.1]
[dhcp.eth0.vendorInfo]: []
[net.change]: [net.eth0.dns2]
[net.eth0.dns1]: [8.8.8.8]
[net.eth0.dns2]: [8.8.4.4]

你可能感兴趣的:(android,android机顶盒开发)