从android执行shell命令我正在尝试从应用程序模拟器终端执行此命令(您可以在谷歌播放中找到它)在这个应用程序中我写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不?
3 回答
![?](http://img1.sycdn.imooc.com/5458477300014deb02200220-100-100.jpg)
喵喵时光机
TA贡献1846条经验 获得超7个赞
您应该获取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);}
![?](http://img1.sycdn.imooc.com/54584cd10001404b02200220-100-100.jpg)
30秒到达战场
TA贡献1828条经验 获得超6个赞
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;
添加回答
举报
0/150
提交
取消