浓缩咖啡无法切换飞行模式 [英] Unable to toggle airplane mode, espresso
问题描述
我正在尝试在 rooted 模拟器的 kitkat版本上切换飞行模式。我正在使用意式浓缩咖啡进行自动化,因此我不得不打开飞机模式并在应用中执行某些步骤
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());
}
}
但是我无法切换到飞行模式,尝试了不同的模式...使用了上述方法,并使用adb命令修改了以下行
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");
有人可以提供代码或adb脚本帮助,通过它我可以打开和关闭飞行模式
can someone help with the code or adb script, by which i can switch on and off the airplane mode
推荐答案
只需按如下所示创建方法并在需要的地方调用:
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);
}
调用方法:
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 ..这是我的要求。
Please note, this will set Data enabled = Off.. which is my requirement.
这篇关于浓缩咖啡无法切换飞行模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!