【问题标题】:Execute a shell command programmatically: why it doesn't work? [duplicate]以编程方式执行 shell 命令:为什么它不起作用? [复制]
【发布时间】:2014-01-18 06:44:28
【问题描述】:

我正在尝试以编程方式执行新命令“ScreenRecorder”kitkat。我刚试过这个:

suProcess = Runtime.getRuntime().exec("su");
process = Runtime.getRuntime().exec("screenrecord /sdcard/Folder/Example.mp4");

但不起作用。在文件夹中,我有文件Example.mp4,但它是空的(0 字节)。为什么?我该如何解决这个问题?问题出在哪里?如果从终端仿真器我写su 并按回车,在写screenrecord /sdcard/Folder/Example.mp4 之后它工作得很好。 Obvisioully 我在 4.4.2 和有根终端上运行。

【问题讨论】:

  • 这样,su 是独立于 screenrecord 执行的
  • 你的设备root了吗?
  • AmanAroraBB 是的,我的设备已植根。请仔细阅读我的问题。 @deviantfan 所以我应该做类似的事情?进程 = Runtime.getRuntime().exec("su"); process = Runtime.getRuntime().exec("screenrecord /sdcard/Folder/Example.mp4");
  • 如果您的su 版本将命令作为参数(即它的工作方式类似于sudo),那么您将使用su screenrecord /sdcard/Folder/Example.mp4。密切关注 logcat 的失败消息。

标签: java android xml root


【解决方案1】:

试试这个

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);
        }
    }

【讨论】:

  • 它不适用于 AT#SERVINFO 命令
【解决方案2】:

它不起作用,因为它是模拟器而不是普通设备。我认为这篇文章在这种情况下可能会有所帮助:http://russelldavis.blogspot.com/2011/01/rooting-android-emulator.html

【讨论】:

  • 我在 Android 设备上的运行时运行应用程序,而不是模拟器。对于普通终端模拟器,我指的是 Google Play 商店中可用的应用程序。
猜你喜欢
  • 1970-01-01
  • 2015-06-27
  • 2019-12-09
  • 2013-01-26
  • 2014-05-17
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
相关资源
最近更新 更多