我正在尝试从应用程序模拟器终端(您可以在 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 的 root 设备上运行。问题出在哪里?我该如何解决?因为终端模拟器可以工作,而 Java 不行?
最佳答案
您应该获取刚刚启动的 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);
}
关于java - 从android执行shell命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20932102/