Unable to toggle airplane mode, espresso

喜夏-厌秋 提交于 2019-12-13 07:53:07

问题


I am trying to toggle airplane mode on kitkat version, on rooted emulator. I am using espresso for automation and i have scenario in which i have to switch on airplane mode and do some kind of steps in the app

I have modified time using the following method :

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());
        }
    }

But I am unable to switch to airplane mode, i have tried different patterns... used the above method and modified following line with the adb commands

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");

can someone help with the code or adb script, by which i can switch on and off the airplane mode


回答1:


Simply create method as follows and call wherever required:

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);
    }

Calling the method:

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();
        }

Please note, this will set Data enabled = Off.. which is my requirement.



来源:https://stackoverflow.com/questions/52878138/unable-to-toggle-airplane-mode-espresso

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!