2015-01-31 4 views
1

Я изучаю андроид и .NETКак запустить ADB команды в VB.NET

Я хотел бы использовать VB.NET для выполнения ADB команды

Например, я хотел бы, чтобы включить или отключить самолет режим с ПК на Android телефон

, такие как

ADB настройки оболочки поместить глобальное airplane_mode_on 1 ADB оболочки утра радиопередачу -a android.intent.action.AIRPLANE_MODE --ez состояние истинного

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

Однако, это действительно не работает, и я не совсем уверен, почему ....

есть все равно, чтобы запустить команду adb с VB.NET?

благодарит

+0

для стартеров '.Arguments' не должна включать в себя' .FileName'.команда, которую вы пытаетесь запустить, это 'adb adb shell ...' –

+0

У меня вопрос. Можем ли мы сделать, как Shell («cmd.exe/c cd adb shell settings put global airplane_mode_on 0», AppWinStyle.Hide). Как это ? –

ответ

0

Вы можете использовать cmd.exe общаться с adb commands увидеть мой пример кода.

Объявляя новый процесс и процесс INFO

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 Осуществляет команду, указанную строку, а затем завершает свою работу.

Теперь установите рабочий каталог для adb.exe в cmd.exe. Не путайте, это то же самое, что и команда 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 
Смежные вопросы