2014-11-25 105 views
0

雖然嘗試將以太網連接提供給運行在Jellybean上的我的Android設備,但我使用了ifconfig和route命令來設置以太網連接。 現在我試圖從Android應用程序執行這些命令,但無法設置IP和網關地址。 有沒有其他的方法來執行這些命令? 我用下面的代碼,在Android應用程序中設置以太網連接

public void root_command(String cmd) 
{ 
try{ 
     Process p=Runtime.getRuntime().exec("su"); 
     DataOutputStream stream=new DataOutputStream(p.getOutputStream()); 
     stream.writeBytes(cmd); 
     stream.writeBytes("exit \n"); 
     p.waitFor(); 
} 
catch(IOException e) 
{ 
} 
catch(InterruptedException e) 
{ 
} 

}

這些命令,

    busybox ifconfig eth0 <ip_address> up 
       busybox route add default gw <gateway_address> eth0 
+0

這是什麼 「以太網連接」?和Android設備我認爲它不是一個電話?你有一個有以太網電纜輸入的設備?你想讓設備在你的局域網中? – cesztoszule 2014-11-25 15:03:35

+0

是的,你是對的。我正在使用具有以太網端口的自定義Android平板電腦。我現在正在開發一個應用程序,可以執行上述busybox命令通過以太網連接到Internet。 – 2014-11-26 05:19:04

回答

0

使用本:

Read.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 

      String[] returncommand = new String[8]; 
      returncommand=read_file(filename); 
      line1.setText(returncommand[0]); 
      line2.setText(returncommand[1]); 
      line3.setText(returncommand[2]); 

      String[] mycommand = {"ifconfig eth0" + " " + returncommand[0] + " " + "netmask" + " " + returncommand[1] + " ", "netcfg eth0 up", 
        "route add default gw" + " " + returncommand[2] + " " + "dev eth0"}; 
      if (returncommand[0] != null & returncommand[1] != null & returncommand[2] != null) 
       RunasRoot(mycommand); 

     } 
    }); 
} 

public void RunasRoot(String[] cmds) { 
    try { 
     java.lang.Process process = Runtime.getRuntime().exec("su"); 
     DataOutputStream outputStream = new DataOutputStream(process.getOutputStream()); 
     for (String tmpcmd : cmds) { 
      outputStream.writeBytes(tmpcmd + "\n"); 
     } 
     outputStream.writeBytes("exit\n"); 
     outputStream.flush(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
相關問題