从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 回答
胡子哥哥
TA贡献1825条经验 获得超6个赞
您应该获取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);}
摇曳的蔷薇
TA贡献1793条经验 获得超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
提交
取消
