代码之家  ›  专栏  ›  技术社区  ›  Ves

当从Android Studio运行我的应用程序时,我可以强制模拟器到顶部吗?

  •  1
  • Ves  · 技术社区  · 7 年前

    这是一个相当小的不便,但我希望Android模拟器弹出到前面,当我开始运行我的应用程序从Android工作室。

    我知道emulator中的“Always on Top”设置,但在编写代码时必须最小化emulator。运行我的应用程序不会还原仿真器。

    2 回复  |  直到 6 年前
        1
  •  2
  •   Alejandro    7 年前

    以下@Ves accepted answer确实有效,但更改了批处理文件,因为它不适合我,下面是我如何编辑批处理文件 基于另一个答案 https://superuser.com/questions/647216/startup-program-bring-to-foreground .

    if ( $args ){
       $processName = $args[0]
    }
    else{
       $processName = "qemu-system-i386"
    }
    
    Add-Type @"
      using System;
      using System.Runtime.InteropServices;
      public class Tricks {
         [DllImport("user32.dll")]
         [return: MarshalAs(UnmanagedType.Bool)]
         public static extern bool SetForegroundWindow(IntPtr hWnd);
      }
    "@
    sleep -sec 1
    $h = (Get-Process $processName).MainWindowHandle
    [void] [Tricks]::SetForegroundWindow($h)
    

        2
  •  1
  •   Ves    7 年前

    为我的问题创建了一个解决方案-我正在Windows10上开发。

    1. 创建PowerShell批处理文件BringProcessToFront.ps1(*注意您的模拟器可能有一个不同的名称,如qemu-system-x86\u 64*)

      if ( $args ){
         $processName = $args[0]
      }
      else{
         $processName = "qemu-system-i386"
      }
      
      $sig = '[DllImport("user32.dll")] public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);'
      Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32
      $hwnd = @(Get-Process $processName)[0].MainWindowHandle
      [Win32.NativeMethods]::ShowWindowAsync($hwnd, 4)
      
    2. 确保您确实可以从命令行运行此脚本。默认的安全性将不允许您运行脚本,作为管理员,您需要允许它们使用 Set-Execution 如果一切正常(并且模拟器正在运行),它将弹出到前端。

      powershell -command C:\users\username\BringProcessToFront.ps1
      
    3. 在Android Studio中创建一个新的外部工具“bringemulatorofront”

      Run>Edit Configuraions
      Expand Android App and select app
      Before launch:  (Hit the green + sign to add a new external tool)
         In the "External Tools Dialog" hit the green + to create a new tool)
             Name: BringEmulatorToFront
             Description: Launch PowerShell to make sure Emulator is visible
             Program: powershell
             Arguments: -command C:\users\username\BringProcessToFront.ps1
             Working directory: C:\users\username
             *IMPORTANT*
              Uncheck the Advanced options Synchronize files and Open console
              (If you leave Open console checked your run will not terminate cleanly)           
      
    4. 运行你的应用程序,看模拟器弹出到前面!

        3
  •  0
  •   Florian Walther    6 年前

    我用 AutoHotkey

    #IfWinActive, ahk_class SunAwtFrame
    $+F10::
    Send, +{F10}
    WinActivate, Android Emulator
    return
    

    您也可以编译它并让它运行,而不必安装AutoHotkey。

        4
  •  -1
  •   Milan Maxa Maksimović    7 年前