Turning airplane mode on via ADB

后端 未结 15 1683
暖寄归人
暖寄归人 2020-12-01 06:43

I\'ve looked all over and I haven\'t found a concrete answer to this question. I\'m hoping to find a way to toggle airplane mode from ADB that doesn\'t involve the GUI in an

15条回答
  •  广开言路
    2020-12-01 07:11

    A working method which I am using in my Mobile Automation Framework:

    Worked with OS 5.x 6.x 7.x and 8.x need to test it against 9.x

    /**
     * Method to set Android device Airplane mode.
     * 
     * @param status
     * 
     *               true - turn on airplane mode false - turn off airplane mode
     */
    public static void setAndroidDeviceAirplaneMode(boolean status) {
        try {
    
            String airplaneModeStatus = "";
            if (status) {
                airplaneModeStatus = "0";
            } else {
                airplaneModeStatus = "0";
            }
            String sdkPath = System.getenv("ANDROID_HOME") + "/platform-tools/";
            Runtime.getRuntime().exec(sdkPath + "adb shell settings put global airplane_mode_on " + airplaneModeStatus);
            Thread.sleep(1000);
            Process process = Runtime.getRuntime()
                    .exec(sdkPath + "adb shell am broadcast -a android.intent.action.AIRPLANE_MODE");
            process.waitFor();
            Thread.sleep(4000);
            if (status) {
                logger.info("Android device Airplane mode status is set to ON");
            } else {
                logger.info("Android device Airplane mode status is set to OFF");
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
            logger.error("Unable to set android device Airplane mode.");
        }
    }
    

提交回复
热议问题