在 Appium Java 客户端中按主页按钮

Press Home button in Appium Java client

我是 appium 的新手。这可能是一个愚蠢的问题。想知道如何使用 java 绑定单击主页按钮。

提前致谢

好吧,如果您想在后台发送应用程序,只需使用 driver.CloseApp() 功能并按 driver.OpenApp()

重新启动它

你也可以使用按键码的方法 下面是代码

Home Menu Button - 82
Back Button - 4
Recent app - 187
public boolean applicationClose(String packageName) {
        appiumDriver.closeApp();
        return true;
    }

or
String ud_id=  ;
String str = "adb -s ud_id shell keyevent KEYCODE_HOME";
        String line;
        try {
            Process p = Runtime.getRuntime().exec(commandStr);
            InputStream is = p.getInputStream();
            InputStreamReader isr = new InputStreamReader(is);
            BufferedReader br = new BufferedReader(isr);

            while ((line = br.readLine()) != null)
                str += line;

            System.out.println("Command output: " + str);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        ReportAppium.getSnapShot(appiumDriver, "run:" + commandStr);
        return str;

请尝试以下代码

driver.pressKeyCode(AndroidKeyCode.HOME);` // it will android device home` button 

如果您想使用 android 设备密钥执行任何其他操作,您可以使用相同的 approach.You 将在 AndroidKeyCode 中获取密钥。

希望这些对你有帮助。

Appium 已弃用 pressKeyCode。相反,使用:

driver.pressKey(new KeyEvent(AndroidKey.HOME));

对于Android:

driver.pressKeyCode(AndroidKeyCode.HOME);

对于IOS:

driver.executeScript("mobile: pressButton", ImmutableMap.of("name", "home"));