【问题标题】:Problem with Runtime.getRuntime().exec()Runtime.getRuntime().exec() 的问题
【发布时间】:2013-04-23 18:36:00
【问题描述】:

我已经植根我的设备,然后在我的应用程序中

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

它工作正常,我的应用程序将是根模式。然后我尝试添加wlan地址空间,但它不起作用,当我在终端中签出时,显示以下错误消息

 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();

当我运行我的应用程序时,toast 显示应用程序是 root 模式但没有添加 wlan0。

【问题讨论】:

  • 当然,如果我写在终端su,busybox ifconfig wlan0 add xxxxxxxx,就可以了,还有一个新的wlan0地址空间。

标签: android


【解决方案1】:

真正不支持“su -c COMMAND”语法。为了更好的可移植性,请使用以下内容:

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

write() 命令不按原样存在,但我相信你会找到如何将流写入它,也许将输出流封装在 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();
    

    【讨论】:

      【解决方案3】:

      这可能是因为当您运行su 时,它会启动一个进程。然后你运行busybox ...,它发生在另一个进程中,它不是作为超级用户启动的。

      试试类似的东西

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

      ,即在单个命令行中执行它。

      【讨论】:

      • 对不起,它不起作用!该应用程序不再是超级用户,也没有添加任何 wlan 地址空间。
      • @user760503:哦,抱歉,我忘记了-c 开关。它应该看起来像这样:exec("su -c \"busybox ifconfig wlan0 add xxxxxxxxxxxx\"")
      • @user760503: man su 表示语法是su -c COMMAND
      • 是的,我在阅读手册页后注意到。但它不起作用。我尝试了以下方式:
      • exec("su -c \"busybox ifconfig wlan0 add xxxxxxxxxxxx\"") 不起作用,以下仅适用于 sudo 部分:String[] cmd = {"su","-c ","busybox","ifconfig wlan0 add xxxxx"} sudo 现在可以工作,但没有添加 wlan!
      猜你喜欢
      • 1970-01-01
      • 2012-04-18
      • 2011-01-09
      • 2015-08-28
      • 1970-01-01
      • 2012-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多