【问题标题】:How to run adb commands in VB.NET如何在 VB.NET 中运行 adb 命令
【发布时间】:2015-01-31 06:32:47
【问题描述】:

我正在学习 android 和 .NET

我想用VB.NET来执行adb命令

例如,我想启用或禁用从 PC 到 Android 手机的飞行模式

比如

adb shell 设置将全局飞机模式设置为 1 adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

Dim psi As New ProcessStartInfo
Dim psi2 As New ProcessStartInfo
psi.WorkingDirectory = "C:\ad\adsystem\adb file"
psi.Arguments = "adb shell settings put global airplane_mode_on 0"
psi.FileName = "adb"
psi.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(psi)

psi2.WorkingDirectory = "C:\ad\adsystem\adb file"
psi2.Arguments = "adb shell am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false"
psi2.FileName = "adb"
psi2.WindowStyle = ProcessWindowStyle.Hidden
Process.Start(psi)

我尝试使用来自

的示例

How to get Output of a Command Prompt Window line by line in Visual Basic?

Shell commands in VB

但是,它并没有真正起作用,我不确定为什么....

是否可以从 VB.NET 运行 adb 命令?

谢谢

【问题讨论】:

  • 对于初学者.Arguments 不应包含.FileName。您尝试运行的命令是 adb adb shell ...
  • 我有一个问题。我们可以像 Shell("cmd.exe /c cd adb shell settings put global plane_mode_on 0", AppWinStyle.Hide) 那样做吗?像这样?

标签: android vb.net adb


【解决方案1】:

您可以使用cmd.exeadb commands 通信,请参阅我的代码示例。

声明新进程和进程信息

Dim My_Process As New Process()
Dim My_Process_Info As New ProcessStartInfo()

使用cmd.exe 作为我的进程的文件名。

My_Process_Info.FileName = "cmd.exe"

在参数中你可以使用这样的 adb 命令。

My_Process_Info.Arguments = "/c adb devices"

/c 执行字符串指定的命令,然后终止。

现在在 cmd.exe 中为 adb.exe 设置 WorkingDirectory。不要混淆,它与cd 命令相同。将我们的工作目录设置为您的 adb.exe 文件所在的位置。

My_Process_Info.WorkingDirectory = "Directory of your adb.exe file"

现在一些必要的进程设置。

My_Process_Info.CreateNoWindow = True  ' Show or hide the process Window
My_Process_Info.UseShellExecute = False ' Don't use system shell to execute the process
My_Process_Info.RedirectStandardOutput = True  '  Redirect (1) Output
My_Process_Info.RedirectStandardError = True  ' Redirect non (1) Output
My_Process.EnableRaisingEvents = True ' Raise events
My_Process.StartInfo = My_Process_Info

设置完成,现在开始处理。

My_Process.Start()

如果你想得到你发送的 adb 命令的回复/结果,你可以使用进程的StandardOutput 属性。

Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Error Output (If any)
Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Standard Output (If any)

示例函数:

Function adb(ByVal Arguments As String) As String
    Try

        Dim My_Process As New Process()
        Dim My_Process_Info As New ProcessStartInfo()

        My_Process_Info.FileName = "cmd.exe" ' Process filename
        My_Process_Info.Arguments = Arguments ' Process arguments
        My_Process_Info.WorkingDirectory = "C:\Users\<Your User Name>\AppData\Local\Android\android-sdk\platform-tools" 'this directory can be different in your case.
        My_Process_Info.CreateNoWindow = True  ' Show or hide the process Window
        My_Process_Info.UseShellExecute = False ' Don't use system shell to execute the process
        My_Process_Info.RedirectStandardOutput = True  '  Redirect (1) Output
        My_Process_Info.RedirectStandardError = True  ' Redirect non (1) Output

        My_Process.EnableRaisingEvents = True ' Raise events
        My_Process.StartInfo = My_Process_Info
        My_Process.Start() ' Run the process NOW

        Dim Process_ErrorOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Error Output (If any)
        Dim Process_StandardOutput As String = My_Process.StandardOutput.ReadToEnd() ' Stores the Standard Output (If any)

        ' Return output by priority
        If Process_ErrorOutput IsNot Nothing Then Return Process_ErrorOutput ' Returns the ErrorOutput (if any)
        If Process_StandardOutput IsNot Nothing Then Return Process_StandardOutput ' Returns the StandardOutput (if any)

    Catch ex As Exception
        Return ex.Message
    End Try

    Return "OK"

End Function

Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)     Handles Button1.Click

    'Usage:

    'Get the list of connected devices.
    MsgBox(adb("/c adb devices")) 

    'Connect your phone wirelessly using wifi (required phone I.P)
    MsgBox(adb("/c adb disconnect 192.168.xx.xx:5555")) 

    'Get the list of connected devices.
    MsgBox(adb("/c adb devices"))

    'Put your phone on airplane mode. 
    MsgBox(adb("/c adb shell settings put global airplane_mode_on 1"))

End Sub

【讨论】:

    猜你喜欢
    • 2014-11-25
    • 1970-01-01
    • 2023-04-10
    • 2012-03-12
    • 1970-01-01
    • 2015-03-26
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    相关资源
    最近更新 更多