从priv-app应用程序运行shell命令

时间:2017-09-28 13:36:04

标签: android shell android-source

我正在尝试从我添加到AOSP(7.1.1)的系统/ priv-app运行shell命令

我试图运行的命令是: ip link add dev can0 type 可以打开can总线。

我将图像构建为 -eng & -userdebug 发布。 该命令在adb shell中运行良好,并按预期成功打开CAN总线。

我的问题是我收到以下错误:

  

无法运行程序" su":错误= 13,权限被拒绝

当我在系统特权java app中尝试以下代码时:

//ArrayList<String> commands is passed into the method
try {
  if (null != commands && commands.size() > 0) {
     Process suProcess = Runtime.getRuntime().exec("su");
     DataOutputStream os = new DataOutputStream(suProcess.getOutputStream());
     for (String currCommand : commands) {
        os.writeBytes(currCommand + "\n");
        os.flush();
     }
     os.writeBytes("exit\n");
     os.flush();
     BufferedReader stderr = new BufferedReader(new InputStreamReader(suProcess.getErrorStream()));
     String line = "";
     String errString = "";
     while ((line = stderr.readLine()) != null) errString += line + "\n";
     suProcess.waitFor();
     if (suProcess.exitValue() != 0)
        throw new Exception(errString);
 } //Handle exception

1 个答案:

答案 0 :(得分:0)

您可以尝试这样做:

Process mProcess = new ProcessBuilder()
                       .command("/system/xbin/su")
                       .redirectErrorStream(true).start();

DataOutputStream out = new DataOutputStream(mProcess.getOutputStream());