无法切换飞行模式、浓缩咖啡

问题描述 投票:0回答:2

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

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

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 脚本吗,通过它我可以打开和关闭飞行模式

android automation adb android-espresso
2个回答
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();
        }

请注意,这将设置数据启用=关闭..这是我的要求。


0
投票

另一个解决方案,我从@Illyct 找到:

InstrumentationRegistry.getInstrumentation().getUiAutomation().executeShellCommand("svc data disable")

请在 https://stackoverflow.com/a/64765567/191761

投票支持他们的答案
© www.soinside.com 2019 - 2024. All rights reserved.