4

I am trying to programmatically set the aeroplane mode on.

Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);

Intent aeroPlaneIntent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
aeroPlaneIntent.putExtra("state", true);
context.sendBroadcast(aeroPlaneIntent);

Setting the Global.AIRPLANE_MODE_ON is failing as if I check the status it returns 0 (OFF)

Trying to broadcast the intent it raises the following exception:

03-12 07:01:18.747: E/AndroidRuntime(1579): java.lang.RuntimeException: Unable to start receiver com.example.toggleaeroplanemode.AeroplaneModeReceiver: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=1579, uid=10050
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2431)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.app.ActivityThread.access$1500(ActivityThread.java:141)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1332)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.os.Looper.loop(Looper.java:137)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invokeNative(Native Method)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at java.lang.reflect.Method.invoke(Method.java:525)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at dalvik.system.NativeStart.main(Native Method)
03-12 07:01:18.747: E/AndroidRuntime(1579): Caused by: java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.AIRPLANE_MODE from pid=1579, uid=10050
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.os.Parcel.readException(Parcel.java:1431)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.os.Parcel.readException(Parcel.java:1385)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.app.ActivityManagerProxy.broadcastIntent(ActivityManagerNative.java:2224)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.app.ContextImpl.sendBroadcast(ContextImpl.java:1046)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:344)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.content.ContextWrapper.sendBroadcast(ContextWrapper.java:344)
03-12 07:01:18.747: E/AndroidRuntime(1579):     at com.example.toggleaeroplanemode.AeroplaneModeReceiver.onReceive(AeroplaneModeReceiver.java:31) 
03-12 07:01:18.747: E/AndroidRuntime(1579):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:2424)
03-12 07:01:18.747: E/AndroidRuntime(1579):     ... 10 more

Is there a way to set the airplane mode ON/OFF? Please help

Mule
  • 75
  • 1
  • 3
  • 9
  • possible duplicate of [turn off airplane mode in Android](http://stackoverflow.com/questions/7066427/turn-off-airplane-mode-in-android) – CommonsWare Mar 12 '14 at 11:29

3 Answers3

4

For little info:

Use this Permission first..

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

further info: Just check this conversation

Community
  • 1
  • 1
Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • 1
    Note that only apps installed on the system partition (e.g., by rooted device users) or by apps signed by the signing key that signed the firmware. Ordinary SDK apps cannot modify the state of airplane mode. – CommonsWare Mar 12 '14 at 11:29
  • Thank you for your help. Sorry I had that permission set, I should have mentioned it. But the conversation you pointed out it was very helpful. It looks like following this path will lead me nowhere as Intent.ACTION_AIRPLANE_MODE_CHANGED is a protected intent. – Mule Mar 12 '14 at 12:04
  • 8
    In Android 5.0 when i trying to send broadcast (ACTION_AIRPLANE_MODE_CHANGED), i got a security exception. I put all requried permissions and my application is a system application. – yasinkafadar Feb 03 '15 at 06:41
  • 1
    Permission android.permission.WRITE_SETTINGS is not a changeable permission type – MaxF Sep 04 '17 at 09:16
1

The below was tested and verified on Android API 23 & 26 Emulators;

The manifest permission needed is as follows;

<uses-permission android:name="android.permission.WRITE_SECURE_SETTINGS" tools:ignore="ProtectedPermissions"/>

The code is as follows;

boolean turnOnOffAirplaneMode(boolean isTurnOn) {
    boolean result = true;
    try {
        Settings.Global.putInt(context.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON, isTurnOn ? 1 : 0);
        // The below old code is now not necessary @ API Level 23, 26 ...
        //Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        //context.sendBroadcast(intent);
    } catch (Exception e) {
        result = false;
    }
    return result;
}

The APK should be signed with the relevant platform key for above to work. In the Emulator test, the APK was signed with Google Sign Keys. The keys depend on the OEM of the device.

Samantha
  • 921
  • 9
  • 12
0
$ adb shell
$ su
# settings put global airplane_mode_on 0
# am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false