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

如何从JavaAndroid设备上运行adb shell screenrecord并结束screenrecording

目前我必须指定--time-limit来结束录制。 如果我尝试在结束之前捕获video,则失败。

有没有办法让adb停止录音? 有没有办法配置线程将CTRL-C发送到shell命令?

这将在无根电话上运行。

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

screenrecord记录过程发送SIGINTSIGHUP会导致它停止录制。

我刚刚在2个单独的adb shell会话中尝试了以下针对无根(Android 6.0手机)的手机:

在会议#1中:

 screenrecord /sdcard/Movies/test.mpg 

在会议#2中:

 pkill -INT screenrecord 

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