How to turn airplanemode ON on Android programatically on API23

2.7k Views Asked by At

I'm trying to turn airplanemode on on Android but I got the following message:

java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE

From my point of view (and some researches):

(1) I'm using all necessary permission to do that:

<uses-permission android:name="android.permission.WRITE_SETTINGS"/>
<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" />

(2) The code is not wrong:

        Settings.Global.putInt(
                context.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", true);
        context.sendBroadcast(intent);

I know that the app needs to be installed as a system app, so I'm installing that under /system/app/my-app/ (I tried /system/priv-app/my-app/ too) and added all the permissions to the folder and to the apk.

The last thing that I tried was the include of android:sharedUserId="android.uid.system" at the AndroidManifest.xml, but doing that the application disappears.

What am I missing here, after all those attempts?

ps: The device is rooted.

Thanks in advance

1

There are 1 best solutions below

1
Tyler Dickson On

It seems although Airplane Mode has become a Constant value as of API Level 17 as per the Android development documentation.

Why are you turning airplane mode on? Is there a certain functionality you are trying to accomplish?

I would've posted this in the comments, but I don't have enough rep... tfw