【问题标题】:Can I run an adb shell command in C# and not have it interrupted when I unplug the USB cable?我可以在 C# 中运行 adb shell 命令并且在拔下 USB 电缆时不会中断它吗?
【发布时间】:2015-12-23 18:52:56
【问题描述】:

我正在使用下面描述的方法运行 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 电缆时,一切都很好。

【问题讨论】:

  • 您似乎对“从 C# 应用程序运行 adb shell 命令”没有任何问题。你为什么把它放在你的标题里?
  • 我很难想出一个没有电话簿那么长的标题。感谢建设性的批评。现在已编辑
  • 当我尝试上述链接中的建议时,我不断收到“nohup: not found”或“daemonize: not found”
  • 对于没有nohup的设备 - 安装busybox并改用busybox nohup

标签: c# android shell console adb


【解决方案1】:

请尝试以下方法:

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,所以这可能需要一些工作才能正常工作

【讨论】:

    猜你喜欢
    • 2013-10-04
    • 2012-03-12
    • 2014-09-24
    • 1970-01-01
    • 2014-01-07
    • 2019-03-16
    • 1970-01-01
    • 2017-04-18
    • 1970-01-01
    相关资源
    最近更新 更多