0

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

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
zzz
  • 497
  • 3
  • 14
  • 32
  • The last two commands seems ok to me, but why you set the time using `date...` and try to set airplane mode with `adb shell settings...`? did you try to remove `adb shell -c` from the string in the outputStream? – TDG Oct 19 '18 at 07:03
  • Yes tried with outputStream.writeBytes("settings put global airplane_mode_on 1"); ... still it is not changing to airplane mode.. – zzz Oct 19 '18 at 08:06
  • I've tried it with a physical device - the device's icon hasn't changed, but the device itself entered airplane mode. Try it again and then try to toggle on mobile data. In my case I got a message that mobile data cannot be turned on while in airplane mode. – TDG Oct 20 '18 at 08:29
  • Its not working – zzz Oct 20 '18 at 13:13

2 Answers2

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.

zzz
  • 497
  • 3
  • 14
  • 32
0

Another solution, which I found from @Illyct:

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

Please upvote their answer at https://stackoverflow.com/a/64765567/191761

Adam Burley
  • 5,551
  • 4
  • 51
  • 72