Q: Toggle airplane mode in Android

D: Did I make a mistake? It's not working.
 public void airplane() {
    boolean isEnabled = Settings.System.getInt(this.getApplicationContext().getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
    Settings.System.putInt(context.getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
    //Settings.System.putInt(this.getApplicationContext().getContentResolver(),Settings.System.AIRPLANE_MODE_ON,isEnabled ? 0 : 1);
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
}
 

Test Case #20


File ID: #5533943-1-cc


Context context = ManageroidApp.getContext();
boolean isEnabled = Settings.System.getInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0) = = 1;
if (isEnabled)
{
    Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", 0);
    context.sendBroadcast(intent);
}

  1. Yeah, it will not work on 4.2.x= because it now requires a WRITE_SECURE_SETTINGS permission that is only available to system apps. Thanks Google - slowly alienating us little guys.
  2. This is no longer possible starting from Android 4.2 and above.
  3. This doesn't work on Android 4.2+

Comments Quality
Accurate?:
Precise?:
Concise?:
Useful?: