Here're some code snippets to help with that.
Check to see if it is enabled or not:
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
To toggle it:
// toggle airplane mode
Settings.System.putInt(
context.getContentResolver(),
Settings.System.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);
To get notifications on state change:
IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");
BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
}
context.registerReceiver(receiver, intentFilter);
Please share if there's an easier way!