【问题标题】:Unable to toggle airplane mode, espresso无法切换飞行模式,浓缩咖啡
【发布时间】:2019-03-23 12:08:32
【问题描述】:

我正在尝试在 rooted 模拟器上切换 kitkat 版本 的飞行模式。我正在使用 espresso 进行自动化操作,我有一个场景,我必须打开飞行模式并在应用程序中执行某些步骤

我使用以下方法修改了时间:

public static void amTime() {

        try {
            Process su = Runtime.getRuntime().exec("su");
            DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

            outputStream.writeBytes("date -s 20181015.070000");
            outputStream.flush();

            outputStream.writeBytes("exit\n");
            outputStream.flush();
            su.wait(2000);
        } catch (Exception e){
            Log.e("Set Time", e.getMessage());
        }
    }

但我无法切换到飞行模式,我尝试了不同的模式...使用上述方法并使用 adb 命令修改以下行

outputStream.writeBytes("mode airplane_mode_on 1");

outputStream.writeBytes("adb shell -c settings put global airplane_mode_on 1");

outputStream.writeBytes("adb shell -c settings put global airplane_mode_on 0");

谁能帮忙写代码或adb脚本,我可以通过它打开和关闭飞行模式

【问题讨论】:

  • 最后两个命令对我来说似乎没问题,但为什么你使用date... 设置时间并尝试使用adb shell settings... 设置飞行模式?您是否尝试从 outputStream 中的字符串中删除 adb shell -c
  • 是的,尝试使用 outputStream.writeBytes("settings put global plane_mode_on 1"); ...仍然没有更改为飞行模式..
  • 我已经在物理设备上试过了——设备的图标没有改变,但设备本身进入了飞行模式。再试一次,然后尝试打开移动数据。在我的情况下,我收到一条消息,指出在飞行模式下无法打开移动数据。
  • 它不工作

标签: android automation adb android-espresso


【解决方案1】:

只需按如下方式创建方法并在需要的地方调用:

public static void setMobileDataEnabled(Context context, boolean enabled) throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException, NoSuchMethodException, InvocationTargetException {
        final ConnectivityManager conman = (ConnectivityManager)  context.getSystemService(Context.CONNECTIVITY_SERVICE);
        final Class conmanClass = Class.forName(conman.getClass().getName());
        final Field connectivityManagerField = conmanClass.getDeclaredField("mService");
        connectivityManagerField.setAccessible(true);
        final Object connectivityManager = connectivityManagerField.get(conman);
        final Class connectivityManagerClass =  Class.forName(connectivityManager.getClass().getName());
        final Method setMobileDataEnabledMethod = connectivityManagerClass.getDeclaredMethod("setMobileDataEnabled", Boolean.TYPE);
        setMobileDataEnabledMethod.setAccessible(true);

        setMobileDataEnabledMethod.invoke(connectivityManager, enabled);
    }

调用方法:

try {
            CommonUtil.setMobileDataEnabled(mActivityTestRule.getActivity().getApplicationContext(),true);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

请注意,这将设置 Data enabled = Off.. 这是我的要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多