1

I am using following method :

private void toggleAirplaneMode() throws Exception {        
    // read the airplane mode setting
    boolean isEnabled = android.provider.Settings.System.getInt(
          getContentResolver(), 
          android.provider.Settings.Global.AIRPLANE_MODE_ON, 0) == 1;

    // toggle airplane mode
    android.provider.Settings.System.putInt(
          getContentResolver(),
          android.provider.Settings.Global.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

    // Post an intent to reload
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", !isEnabled);
    sendBroadcast(intent);
}

And getting following exception :

12-30 09:49:05.875: D/tag(5876): android.os.Parcel.readException(Parcel.java:1540)
12-30 09:49:05.875: D/tag(5876): android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
12-30 09:49:05.875: D/tag(5876): android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
12-30 09:49:05.875: D/tag(5876): android.content.ContentProviderProxy.call(ContentProviderNative.java:643)
12-30 09:49:05.875: D/tag(5876): android.provider.Settings$NameValueCache.putStringForUser(Settings.java:1094)
12-30 09:49:05.876: D/tag(5876): android.provider.Settings$Global.putStringForUser(Settings.java:6827)
12-30 09:49:05.876: D/tag(5876): android.provider.Settings$Global.putString(Settings.java:6811)
12-30 09:49:05.876: D/tag(5876): android.provider.Settings$Global.putInt(Settings.java:6905)

where i am getting wrong??Is there any other way out???

user2014
  • 87
  • 1
  • 1
  • 8

2 Answers2

1
// 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();

Put String insted int and don't forget to add permissions

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

Hope it may help u

Abhishek Chaubey
  • 2,960
  • 1
  • 17
  • 24
1

From Android 4.2 Aeroplane mode changing intent is protected only System apps can change it.so it doesn't work for other apps.

For more check this Aeroplane mode Intent

below 4.2 you can chage aeroplane mode like

You can get the Aeroplane mode status like

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;
    }
}

You can change the aeroplane mode like

@SuppressWarnings("deprecation")
public static void switchAeroplaneMode(Context context, boolean isEnabled) {
    if (isEnabled == isAirplaneModeOn(context)) {
        return;
    }
    if (isAeroplaneModeSupports()) {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, isEnabled ? 1 : 0);

        // Post an intent to reload.
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", !isEnabled);
        context.sendBroadcast(intent);
    }
}

public static boolean isAeroplaneModeSupports() {
    return Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1;
}

And add Permission in manifest

<uses-permission android:name="android.permission.WRITE_SETTINGS" />
kalyan pvs
  • 14,486
  • 4
  • 41
  • 59
  • so you mean above 4.2 i can not change airplane mode?? And I have rooted device. – user2014 Dec 30 '14 at 10:19
  • @user2014 for rooted device you can change..check here http://stackoverflow.com/questions/13766909/how-to-programmatically-enable-and-disable-flight-mode-on-android-4-2/15877584#15877584 – kalyan pvs Dec 30 '14 at 10:21