2011-08-02 76 views
2

我已經紮根在我的設備,然後在我的應用問題與調用Runtime.getRuntime()。EXEC()

Process p = Runtime.getRuntime().exec("su"); 

,它做工精細,我的應用程序將是根模式。然後我嘗試添加無線局域網的地址空間,但它不工作,當我在終端退房,以下錯誤消息顯示

busybox ifconfig there is not a new wlan address space. 

我嘗試用以下方式:

Process p = Runtime.getRuntime().exec("su"); 
p = Runtime.getRuntime().exec("busybox ifconfig wlan0 add xxxxxxxxxxxx"); 
p.waitfor(); 

當我運行我的應用程序,吐司顯示該應用程序是根模式,但沒有添加wlan0。

+0

當然,如果我在終端蘇寫的busybox的ifconfig爲wlan0添加XXXXXXXX,它工作正常,並有一個新的爲wlan0地址空間。 – user760503

回答

0

這可能是因爲當您運行su時,它會啓動一個進程。然後你運行busybox ...,它發生在另一個不以超級用戶身份啓動的進程中。

嘗試像

Process p = Runtime.getRuntime().exec(new String[] {"su", "-c", "busybox ifconfig wlan0 add xxxxxxxxxxxx"}); 

,即在單個命令行執行它。

+0

對不起,它不工作!沒有更多的應用程序是超級用戶,並且沒有添加wlan地址空間。 – user760503

+0

@ user760503:哦,對不起,我忘了'-c'開關。它應該看起來像這樣:'exec(「su -c \」busybox ifconfig wlan0 add xxxxxxxxxxxx \「」)' – Andy

+0

@ user760503:'man su'告訴語法是'su -c COMMAND'。 – Andy

1

不支持「su -c COMMAND」語法。爲了更好的便攜性,使用這樣的:

p = Runtime.getRuntime().exec("su"); 
stream = p.getOutputStream(); 
stream.write("busybox ifconfig wlan0 add xxxxxxxxxxxx"); 

寫()命令不存在的,是的,但我敢肯定,你會發現如何將流寫入到它,也許封裝輸出流在BufferedOutputWriter左右。

2

,因爲以「busybox」開頭的進程與以「su」開頭的進程不是相同的一個 。你應該是這樣的:

Process process = Runtime.getRuntime().exec("su"); 
OutputStream os = process.getOutputStream(); 
DataOutputStream dos = new DataOutputStream(os); 
dos.writeBytes("busybox ifconfig wlan0 add xxxxxxxxxxxx" + "\n"); 
dos.flush(); 
相關問題