c# - 我可以在 C# 中运行 adb shell 命令,并且在拔下 USB 电缆时不会中断它吗?

标签 c# android shell console adb

我正在使用下面描述的方法运行 adb shell 命令

System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe", "/c " + cmd);
procStartInfo.RedirectStandardInput = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = procStartInfo;
proc.Start();

在 Android 手机上,我正在启动一个录制视频的应用程序,因此 cmd = "adb shell am startservice -n com.xxx.xxx/.xxx"。

问题是我开始录制视频后需要拔掉USB线。如果我这样做,生成的视频文件将无法播放。有时它是 0KB。如果我保持 USB 数据线连接,则视频没问题。

有没有办法执行adb命令,以便在拔下USB线后视频继续录制?当我从命令提示符输入命令并拔下 USB 数据线时,一切正常。

最佳答案

请尝试以下操作:

string cmd = " shell am startservice -n com.xxx.xxx/.xxx";
using (Process proc = new process())
{
System.Diagnostics.ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
procStartInfo.RedirectStandardInput = true;
procStartInfo.RedirectStandardOutput = true;
procStartInfo.RedirectStandardError = true;
procStartInfo.UseShellExecute = false;
procStartInfo.CreateNoWindow = true;
proc.StartInfo = procStartInfo;
proc.Start();
proc.StandardInput.Writeline("adb devices");
proc.StandardInput.Writeline("adb shell getprop dhcp.wlan0.ipaddress"); // this will not work on some newer os
string deviceip = proc.StandardOutput.Readline();
proc.StandardInput.Writeline("adb tcpip 5555"); // this will turn on ADB to wifi on port 5555
proc.StandardInput.Writeline("adb connect " + deviceip + " 5555"); // this allows you to at this point unplug your device
proc.StandardInput.Writeline("adb -s " + deviceip + ":5555" +  cmd);  // this connects running the command on the specific ip address so when you disconnect usb it will continue creating movie
proc.WaitForExit();
} // this might not be correct wait command

我使用的主要语言是 Visual Basic,因此这可能需要一些工作才能正常工作

关于c# - 我可以在 C# 中运行 adb shell 命令,并且在拔下 USB 电缆时不会中断它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34441978/

相关文章:

c# - 在 Windows 启动时启动 C# 应用程序并保持焦点

c# - WindowsIdentity : Exception: System. Security.SecurityException: 用户名或密码不正确

java - .java 和 .xml 文件替换为一些随机代码

python - 将 python 命令行工具分发给其他人的最简单方法?

linux - 增加变量号并分配新值

python - 使用 cmd.exe 时找不到脚本文件

c# - 使用 WCF 异步有好处吗?

c# - .NET 随机程序集在磁盘上被修改

android - 使用 Prop 将标题添加到导航栏 ReactNative

java - RecyclerView Item 不包裹 ListView 的高度