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

How can I modify the code or adb script, so I can switch on and off the airplane mode?

5
  • 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, 2018 at 7:03
  • Yes tried with outputStream.writeBytes("settings put global airplane_mode_on 1"); ... still it is not changing to airplane mode..
    – zzz
    Oct 19, 2018 at 8: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, 2018 at 8:29
  • Its not working
    – zzz
    Oct 20, 2018 at 13:13
  • 2
    This looks like a duplicate of: Testing internet connectivity error handling in Android Studios with Espresso on Emulator. A recent answer was given below (and similarly on related questions) that indicate to me that we should be closing several questions in favour of the proposed duplicate here.
    – halfer
    Nov 17, 2023 at 16:41

1 Answer 1

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.