Home > Tutorials > Controlling Airplane Mode

Controlling Airplane Mode

Just having fun with some system settings, specifically, Airplane Mode.

Airplane Mode: ON

Airplane Mode: ON

So what I’m going to do is to create an application that does switch/toggle the Airplane Mode on the phone.

w/ Airplane Mode ON

w/ Airplane Mode ON

w/ Airplane Mode OFF

w/ Airplane Mode OFF

Using a TextView to display the current state of Airplane Mode, and a ToggleButton to switch/toggle the current mode.

This is how it’s done:

package pete.android.study;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Main extends Activity {
    // constants
	static final String STATUS_ON = "Airplane Mode: ON";
	static final String STATUS_OFF = "Airplane Mode: OFF";

	static final String TURN_ON = "Turn ON";
	static final String TURN_OFF = "Turn OFF";

	// controls
	TextView tvStatus;
	ToggleButton togState;

    public void onCreate(Bundle savedInstanceState) {
        // load controls
        tvStatus = (TextView)findViewById(R.id.tvStatus);
        togState = (ToggleButton)findViewById(R.id.togState);
        // update UI at first time loading
        // set click event for button
        togState.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// check current state first
				boolean state = isAirplaneMode();
				// toggle the state
				// update UI to new state

    public void toggleAirplaneMode(boolean state) {
    	// toggle airplane mode
    	Settings.System.putInt(this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, state ? 0 : 1);

    	// broadcast an intent to inform
    	Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    	intent.putExtra("state", !state);

    public void updateUI(boolean state) {
    	if(state) {
    	} else {

    public boolean isAirplaneMode() {
    	return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;

Pretty much easy to understand, comments do the explanation itself. Guess you have it!
Just a note: the “sendBroadcast()” will notify the system about settings/config changed; hence, you can see the top icon on the status bar (the ‘Airplane‘ icon) update as you switch between the modes.

Browse Source Code from Android-Newbie Repository

Pete Houston

Categories: Tutorials Tags: , , ,
  1. April 13, 2012 at 3:34 pm

    You forget to say about manifest file: add android.permission.WRITE_SETTINGS

  2. October 17, 2011 at 10:31 pm

    @Steven: I’ve updated repository for source code. Check it out!

    • Steven
      October 18, 2011 at 10:32 am

      Hi, Pete.
      Thanks a lot.

  3. Steven
    October 17, 2011 at 10:06 am

    Hi, Pete,
    I am interested how it’s done,
    but can’t see the code on the website.
    Can you send this project for reference ?
    Many thanks.

    My email:aeioutop@yahoo.com.tw

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: