从android执行shell命令 [英] execute shell command from android

查看:56
本文介绍了从android执行shell命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从应用程序模拟器终端(您可以在 google play 中找到它)在此应用程序中执行此命令,我编写 su 并按 enter,所以写:

I'm trying to execute this command from the application emulator terminal (you can find it in google play) in this app i write su and press enter, so write:

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

再按回车开始录屏,使用android kitkat的新功能.

and press again enter and start the recording of the screen using the new function of android kitkat.

所以,我尝试使用这个从java执行相同的代码:

so, i try to execute the same code from java using this:

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

但是不工作,因为文件没有被创建.显然,我在安装了 android kitkat 的有根设备上运行.问题出在哪儿?我该如何解决?因为从终端模拟器工作而在 Java 中不工作?

But don't work because the file is not created. obviously i'm running on a rooted device with android kitkat installed. where is the problem? how can i solve? because from terminal emulator works and in Java not?

推荐答案

你应该获取刚刚启动的 su 进程的标准输入并在那里写下命令,否则你正在运行命令当前UID.

You should grab the standard input of the su process just launched and write down the command there, otherwise you are running the commands with the current UID.

试试这样的:

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

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

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

这篇关于从android执行shell命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆