Airplane Mode ON/OFF in Android with example


             The purpose behind Airplane mode is to cut of all signal transmissions from mobile phones in flights as these could cause signal interference  with the communication and guidance systems on the aircraft.
In such cases people can put their phone on Flight mode instead of switching it off and continue using other features.
             Airplane mode is an setting in any mobile that suspends all the signal transmitting functions like phone calls, messaging,  Bluetooth, WI-FI etc. But it permits to use all other features of mobile phones that does not require signal transmission such as games, music players etc.Other names include flight modeaeroplane modeoffline mode, and standalone mode.
   List of radios/signals affected while turning ON airplane mode can be obtained through AIRPLANE_MODE_RADIOS. The purpose of this tutorial is to show you how to switch Airplane mode ON/OFF in android programatically. In Android Airplane Mode is something like a toggle future, it should be set to 1 - ON, 0 - OFF. 

Get Airplane Mode Current State
      Since its a toggle feature we need to know the current status before modifying, it can be done through getInt() method on Settings.SystemThis method requires a ContentResolver which can be obtained from the context and the system setting to be set - AIRPLANE_MODE_ON setting as shown in the below code snippet.

boolean isEnabled = Settings.System.getInt(this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1;

Set Airplane Mode State
           In the same way putInt() can be used to modify state of Airplane mode  as shown  below:
Airplane Mode ON
Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON,1);
Airplane Mode OFF
Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON,0);

Broadcast the modified State            
           After changing the system settings, before airplane mode takes effect we  must broadcast  that the mode has changed.  Create an Intent with the Intent.ACTION_AIRPLANE_MODE_CHANGED action., add the airplane mode “state” to the Intent and then broadcast the message.  Failing to do so leaves the device in its current airplane mode state
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", true/false);
sendBroadcast(intent);
Final but most important step is to add permission for Flight Mode in AndoidManifest.xml

Android Permission for Flight Mode:
<uses-permission android:name="android.permission.WRITE_SETTINGS" />

Thats it, these steps will get this done. 
Note: This method is deprecated in API level 17



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

 <Button
     android:id="@+id/ModeON"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentTop="true"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="34dp"
     android:onClick="airPlanemodeON"
     android:text="Airplane Mode ON" />

 <Button
     android:id="@+id/ModeOFF"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_below="@+id/buttonStop"
     android:layout_centerHorizontal="true"
     android:layout_marginTop="116dp"
     android:onClick="airPlanemodeOFF"
     android:text="Airplane Mode OFF" />


</RelativeLayout>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.airplanemodeonoff"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

     <uses-permission android:name="android.permission.WRITE_SETTINGS" />
    
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.airplanemodeonoff.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>


</manifest>

MainActivity.java

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


@SuppressWarnings("deprecation")
public void airPlanemodeON(View v) {
boolean isEnabled = Settings.System.getInt(this.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
if (isEnabled == false) {
modifyAirplanemode(true);
Toast.makeText(getApplicationContext(), "Airplane Mode ON",
Toast.LENGTH_LONG).show();
}
}

public void airPlanemodeOFF(View v) {
boolean isEnabled = Settings.System.getInt(this.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
if (isEnabled == true)// means this is the request to turn ON AIRPLANE mode
{
modifyAirplanemode(false);
Toast.makeText(getApplicationContext(), "Airplane Mode OFF",
Toast.LENGTH_LONG).show();
}
}

public void modifyAirplanemode(boolean mode) {
Settings.System.putInt(getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, mode ? 1 : 0);// Turning ON/OFF Airplane mode.

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);// creating intent and Specifying action for AIRPLANE mode.
intent.putExtra("state", !mode);// indicate the "state" of airplane mode is changed to ON/OFF
sendBroadcast(intent);// Broadcasting and Intent

}

}

Source Code of this Application
AirplaceModeOnOff.zip




Reactions:

5 comments :

  1. Is it not possible anymore using Kit Kat version?

    ReplyDelete
  2. Does not work after API 17

    ReplyDelete
  3. it is still possible, you can use
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
    Settings.System.putInt(context.getContentResolver(),
    Settings.System.AIRPLANE_MODE_ON, toState);
    } else {
    Settings.Global.putInt(context.getContentResolver(),
    Settings.Global.AIRPLANE_MODE_ON, toState);
    }

    ReplyDelete
  4. Is there any Profile application for Kitkat that can turn on/off Airplane mode now ?

    ReplyDelete
  5. It works on 4.0.4!!
    Very thanks!!!

    ReplyDelete