在安卓手机上执行Shell命令

55

我正在尝试从应用程序模拟器终端执行此命令(您可以在Google Play中找到它),在该应用程序中,我输入su并按enter,然后输入:

screenrecord --time-limit 10 /sdcard/MyVideo.mp4

再次按enter开始录制屏幕,并使用Android KitKat的新功能。

因此,我尝试使用以下内容从Java执行相同的代码:

Process su = Runtime.getRuntime().exec("su");
Process execute = Runtime.getRuntime().exec("screenrecord --time-limit 10 /sdcard/MyVideo.mp4");

但是无法工作,因为文件尚未创建。显然我正在运行安装了Android KitKat的rooted设备上。问题出在哪里?我该如何解决?因为从终端模拟器中可以工作,但是在Java中却不行?

6个回答

79
你应该获取刚启动的su进程的标准输入并在那里记录命令,否则你正在使用当前UID运行命令。

尝试这样做:

try{
    Process su = Runtime.getRuntime().exec("su");
    DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

    outputStream.writeBytes("screenrecord --time-limit 10 /sdcard/MyVideo.mp4\n");
    outputStream.flush();

    outputStream.writeBytes("exit\n");
    outputStream.flush();
    su.waitFor();
}catch(IOException e){
    throw new Exception(e);
}catch(InterruptedException e){
    throw new Exception(e);
}

3
有些版本的 su 命令可以在命令行中接受参数,因此你可以这样执行: exec("su -c screenrecord /sdcard/foo.mp4") - fadden
7
没有root权限也能进行录音吗? - Hendra Anggrian
2
非常感谢您提供的这个代码片段,只是补充一下我在这篇文章中遗漏的是命令字符串末尾必须有\n - rexxar
1
执行exec()时出错。命令:[su] 工作目录:null 环境变量:null - Sanjay Bhalani
1
我运行这些代码,得到了“su error=13,permission denied”的错误提示,需要使用root权限吗? - Bryant
显示剩余2条评论

35

根据@CarloCannas的代码修改:

public static void sudo(String...strings) {
    try{
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        for (String s : strings) {
            outputStream.writeBytes(s+"\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        outputStream.close();
    }catch(IOException e){
        e.printStackTrace();
    }
}

(您可以寻找更好的位置来使用 outputStream.close()

用法示例:

private static void suMkdirs(String path) {
    if (!new File(path).isDirectory()) {
        sudo("mkdir -p "+path);
    }
}

更新: 要获取结果(即标准输出),请使用:

public static String sudoForResult(String...strings) {
    String res = "";
    DataOutputStream outputStream = null;
    InputStream response = null;
    try{
        Process su = Runtime.getRuntime().exec("su");
        outputStream = new DataOutputStream(su.getOutputStream());
        response = su.getInputStream();

        for (String s : strings) {
            outputStream.writeBytes(s+"\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        res = readFully(response);
    } catch (IOException e){
        e.printStackTrace();
    } finally {
        Closer.closeSilently(outputStream, response);
    }
    return res;
}
public static String readFully(InputStream is) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buffer = new byte[1024];
    int length = 0;
    while ((length = is.read(buffer)) != -1) {
        baos.write(buffer, 0, length);
    }
    return baos.toString("UTF-8");
}

一个可静默关闭多个Closeables的实用程序(Soсket可能不是Closeable)是:

public class Closer {
// closeAll()
public static void closeSilently(Object... xs) {
    // Note: on Android API levels prior to 19 Socket does not implement Closeable
    for (Object x : xs) {
        if (x != null) {
            try {
                Log.d("closing: "+x);
                if (x instanceof Closeable) {
                    ((Closeable)x).close();
                } else if (x instanceof Socket) {
                    ((Socket)x).close();
                } else if (x instanceof DatagramSocket) {
                    ((DatagramSocket)x).close();
                } else {
                    Log.d("cannot close: "+x);
                    throw new RuntimeException("cannot close "+x);
                }
            } catch (Throwable e) {
                Log.x(e);
            }
        }
    }
}
}

您,先生,真棒。是的。 - Chisko

5
Process p;
StringBuffer output = new StringBuffer();
try {
    p = Runtime.getRuntime().exec(params[0]);
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(p.getInputStream()));
    String line = "";
    while ((line = reader.readLine()) != null) {
        output.append(line + "\n");
        p.waitFor();
    }
} 
catch (IOException e) {
    e.printStackTrace();
} catch (InterruptedException e) {
    e.printStackTrace();
}
String response = output.toString();
return response;

我无法使用上述命令执行器 am start -n com.qop.tabletApp.activity.ConfigurationAndSetup 启动应用程序。 - Sagar

0
晚回复了,但这会对某些人有好处。您可以在exec()方法中使用sh命令。 以下是我的示例:
try {
    File workingDirectory = new File(getApplicationContext().getFilesDir().getPath());
    Process shProcess = Runtime.getRuntime().exec("sh", null, workingDirectory);
    try{
        PrintWriter outputExec = new PrintWriter(new OutputStreamWriter(shProcess.getOutputStream()));
        outputExec.println("PATH=$PATH:/data/data/com.bokili.server.nginx/files;export LD_LIBRARY_PATH=/data/data/com.bokili.server.nginx/files;nginx;exit;");
        outputExec.flush();
    } catch(Exception ignored){  }
    shProcess.waitFor();
} catch (IOException ignored) {
} catch (InterruptedException e) {
    try{ Thread.currentThread().interrupt(); }catch(Exception ignored){}
} catch (Exception ignored) { }

我用它做了什么?

首先,我调用 shell ,然后在其中更改(设置)必要的环境,最后使用它启动我的nginx。

这适用于未经root处理的设备。

问候。


0

0

答案@18446744073709551615很棒,但如果安卓手机已经通过更新版本的Magisk获取了root权限,则有更简单的方法可供使用,即使用--command选项来运行一次性命令,这样可以使代码更简单且更少出错。

import org.apache.commons.lang3.ArrayUtils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public static List<String> sudo(String[] cmd) {
        List<String> res = new ArrayList<String>();
        try {
            String[] fullCmd = ArrayUtils.addAll(new String[]{"su", "--command"}, cmd);
            Process su = Runtime.getRuntime().exec(fullCmd);
            BufferedReader stdInput = new BufferedReader(new
                    InputStreamReader(su.getInputStream()));

            BufferedReader stdError = new BufferedReader(new
                    InputStreamReader(su.getErrorStream()));

            String s;
            while ((s = stdInput.readLine()) != null)
                res.add(s);
            while ((s = stdError.readLine()) != null)
                res.add(s);

        } catch (IOException e) {
            e.printStackTrace();
        }
        return res;
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接