Java 以编程方式执行shell命令:为什么它不';不行?
我试图以编程方式执行新命令“ScreenRecorder”kitkat。我刚试过这个:Java 以编程方式执行shell命令:为什么它不';不行?,java,android,xml,root,Java,Android,Xml,Root,我试图以编程方式执行新命令“ScreenRecorder”kitkat。我刚试过这个: suProcess = Runtime.getRuntime().exec("su"); process = Runtime.getRuntime().exec("screenrecord /sdcard/Folder/Example.mp4"); 但是不起作用。在文件夹中,我保存了文件Example.mp4,但它是空的(0字节)。为什么?我怎样才能解决这个问题?问题在哪里?如果我从终端仿真器写入su,然后
suProcess = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("screenrecord /sdcard/Folder/Example.mp4");
但是不起作用。在文件夹中,我保存了文件
Example.mp4
,但它是空的(0字节)。为什么?我怎样才能解决这个问题?问题在哪里?如果我从终端仿真器写入su
,然后按enter键,然后在写入屏幕记录/sdcard/Folder/Example.mp4
后,它工作得很好。很明显,我在4.4.2和根终端上运行。它不工作,因为它是一个模拟器,而不是普通设备。我认为这篇文章在这种情况下会有所帮助:试试这个
public static void ScreenRecord() throws Exception
{
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);
}
}
这样,su将独立于ScreenRecord执行。您的设备是否已根目录?AmanAroraBB是的,我的设备已根目录。请仔细阅读我的问题@deviantfan那么我应该做些什么?process=Runtime.getRuntime().exec(“su”);process=Runtime.getRuntime().exec(“screenrecord/sdcard/Folder/Example.mp4”);如果您的
su
版本将命令作为参数(即它的工作方式类似于sudo
),那么您将使用su screenrecord/sdcard/Folder/Example.mp4
。注意logcat的失败消息。我正在Android设备上运行应用程序,而不是在模拟器上运行。对于mean terminal emulator,我指的是Google Play Store中提供的应用程序。它不适用于AT#SERVINFO命令