如何从 Java 在 Android 设备上 运行 adb screenrecord 并结束屏幕录制?

How to can I run adb screenrecord on a Android Device from Java and end the screenrecording?

我如何才能 运行 adb shell screenrecordAndroid 设备上从 Java 结束 screenrecording

目前我必须指定--time-limit来结束录制。如果我尝试在视频结束前捕捉视频,则会失败。

有没有办法让adb停止记录?有没有办法配置线程发送 CTRL-Cshell 命令?

这是 运行 在无根电话上。

代码

videoExecutor.submit(() -> {
          //Start recording video
        String recordVideo = "screenrecord --time-limit " + testRun.getVideoLength() + " " + "/sdcard/" + logDate + ".mp4";
    try {
      device.executeShell(recordVideo);
  } catch (Exception e1) {
    e1.printStackTrace();
  }
    sleep(testRun.getVideoLength()*ONE_SECOND + TWO_SECONDS);  //gotta match the --time-limit above.

RemoteFile remote = new RemoteFile("/sdcard/" + logDate + ".mp4");
File local = new File(videoPath); //Save file to local machine
          if(!local.getParentFile().exists()) {
              local.getParentFile().mkdirs();
          }

    try {
      //Copy video from phone to computer
        try {
                  device.pull(remote, local);
              } catch (Exception e) {
                  e.printStackTrace();
              }
  } catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }

      });

SIGINTSIGHUP 发送到 screenrecord 进程会导致它停止记录。

我刚刚在 unrooted (stock Android 6.0) phone 的 2 个单独的 adb shell 会话中尝试了以下操作:

在会话 #1 中:

screenrecord /sdcard/Movies/test.mpg

在会话 #2 中:

pkill -INT screenrecord

并且 screenrecord 捕获在发出 pkill 命令后停止