从Android执行Shell命令
我正在尝试在我编写su
并按下的应用程序中从应用程序仿真器终端(您可以在Google Play中找到它)执行此命令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的有根设备上运行。问题出在哪里?我该如何解决?因为从终端仿真器工作并且在Java中不行?
-
你应该获取su刚刚启动的进程的标准输入并在此处写下命令,否则你将使用current运行命令
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); }