Question

Can any one tell me how to put a phone in Airplane mode programmatically with a single click on button in android?

Was it helpful?

Solution

See the blog article http://dustinbreese.blogspot.in/2009/04/andoid-controlling-airplane-mode.html ,

Works only upto API 16

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

where isEnabled is whether airplane mode is enabled or not.

OTHER TIPS

try this to put your phone on airplane mode.

// read the airplane mode setting
boolean isEnabled = Settings.System.getInt(
      getContentResolver(), 
      Settings.System.AIRPLANE_MODE_ON, 0) == 1;

// toggle airplane mode
Settings.System.putInt(
      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);

Also make sure you have the WRITE_SETTINGS permission

 this code to make phone silent

        AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
        am.setRingerMode(AudioManager.RINGER_MODE_SILENT);

there is also vibrate mode and normal mode

       am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
       am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top