31

Is there a way to disable or enable Flight Mode on Android 4.2?

I use this code that works only for previous Android versions:

android.provider.Settings.System.putInt(
    c.getContentResolver(),   
    android.provider.Settings.System.AIRPLANE_MODE_ON, enable ? 0 : 1
);
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Meroelyth
  • 5,310
  • 9
  • 42
  • 52

4 Answers4

44

There exist a simple workaround on rooted devices.

To enable Airplane Mode the following root shell commands can be used:

settings put global airplane_mode_on 1
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

To disable Airplane Mode these root shell commands can be used:

settings put global airplane_mode_on 0
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false

Info taken from here

Disclaimer: This info is provided "as-is" without any form of warranty.

DavisNT
  • 686
  • 7
  • 9
24

This is no longer possible, except by apps that are signed by the firmware signing key or are installed on the system partition (typically by a rooted device user).

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
16

You can't, as written in Official Android 4.2 API Documentation

Some device settings defined by Settings.System are now read-only. If your app attempts to write changes to settings defined in Settings.System that have moved to Settings.Global, the write operation will silently fail when running on Android 4.2 and higher. Even if your value for android:targetSdkVersion and android:minSdkVersion is lower than 17, your app is not able to modify the settings that have moved to Settings.Global when running on Android 4.2 and higher.

However, if you are the OS Developer, you can write it when you set these permissions

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

Then to write and read,

// To Write
Settings.Global.putString(getContentResolver(), "airplane_mode_on", "1");

// To Read
String result = Settings.Global.getString(getContentResolver(), Settings.Global.AIRPLANE_MODE_ON);
Toast.makeText(this, "result:"+result, Toast.LENGTH_SHORT).show();
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
Sruit A.Suk
  • 7,073
  • 7
  • 61
  • 71
  • Hmmmmm, but this method just add in Android 4.2 – Sruit A.Suk Dec 12 '12 at 19:12
  • 1
    Oh, yes. It can't. It can only if only you give the permission android.permission.WRITE_SECURE_SETTINGS That's mean you must an os dev,, and I'm so I didn't notice it. – Sruit A.Suk Dec 12 '12 at 20:08
  • @ToonSuperLove,Are you sure these Settings.Global will write device settings airplane mode.I have an similar task to always keep air plane mode in off.reply me ASAP. – Senthil Mg Apr 05 '13 at 20:43
  • @ToonSuperLove, I tried you snippet, having problem on adding permission WRITE_SECURE_SETTINGS at manifest..Let me guide to achieve the result to disable airplane mode – Senthil Mg Apr 06 '13 at 06:45
  • @SenthilMg , did you find a solution? How can I create a **System app** and use the **WRITE_SECURE_SETTINGS** permission? – Amitai Fensterheim Jul 04 '16 at 20:53
  • 2
    What is an OS Developer exactly? Anyone who has a rooted phone? How can I get this to work? – Zaz Feb 11 '17 at 22:59
  • For those that don't know how to get the permission, come here: https://github.com/aosp-mirror/platform_frameworks_base/blob/master/core/res/AndroidManifest.xml. Look for the permission. You'll notice is says (currently) "signature|privileged|development". So you either sign the app with the firmware key, you install is as a privileged app, or you keep debug mode enabled on it for the permission to be granted (hopefully I didn't make any mistake here). – Edw590 Nov 28 '21 at 20:15
  • what the...? Only Supported for OS Dev...? Omg... I feel a nightmare for several limitation we achieved so far in Android Dev then. – gumuruh Jan 02 '22 at 08:13
0

For KitKat, you have to add android:sharedUserId="android.uid.system" in manifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.airplanesetting"
    android:versionCode="1"
    android:versionName="1.0"
    android:sharedUserId="android.uid.system" >
Andrew T.
  • 4,701
  • 8
  • 43
  • 62
  • 2
    I get "installation error: INSTALL_FAILED_SHARED_USER_INCOMPATIBLE" from eclipse when trying to install the apk with this in there – CompEng88 Aug 14 '14 at 13:41
  • 2
    Changing the sharedUserId won't work unless you are also signed with the same key, aka using the system's keystore – Blundell Jun 04 '20 at 12:41