I'm currently trying to toggle the Airplane mode programmatically using Android version 6.0.

I've done quite a lot research and some people say it's not possible anymore since Version 4.2, while others say there still are some workarounds, which I've tried but didn't work for me.

What I've tried last is the following:

public static boolean isAirplaneModeOn(Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            return Settings.System.getInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0) != 0;
        } else {
            return Settings.Global.getInt(context.getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
        }
    }
    public static void setSettings(Context context, int value) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
            Settings.System.putInt(
                    context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, value);
        } else {
            Settings.Global.putInt(
                    context.getContentResolver(),
                    Settings.Global.AIRPLANE_MODE_ON, value);
        }
    }

But here I get the error Permission denial: writing to settings requires:android.permission.WRITE_SECURE_SETTINGS I know that I can change these settings on the emulator, but is it also possible to change these settings on my external device? In another thread some people said that for some devices it is possible, but I couldn't figure out any specific information about my Device (HUAWEI P8 Light). So my question is, is it possible to do so, or is there even a better way to do it? I've also read some about using ADB to toggle airplane mode, but there they said that this would need the same permissions. I hope somebody can help me out. :)

1 Answers

0
SiMo On Best Solutions

this is not available for non system applications anymore , because Google denied this on API level. It's possible for data connection to be enabled/disabled on rooted phones tho