651.288.7000 info@intertech.com

Recently, I have been assisting a high school student – Ryan – with a school science project.  This bright and talented young man’s project (which I have agreed not to disclose in detail) is building a mobile application for the iPhone and Android platforms.  Wow!  I think my high school science project involved trying to create electricity with a potato.  Of course my IQ and talents were more aligned with the vegetable family … enough said on that matter.  Ryan on the other hand, has started to unravel the mysteries of two mobile platforms and coding on each of them by himself with just a little help from his dad and uncle.  This guy’s got oodles of intelligence and a bright future ahead of him.  Fellow Android developers and users, rest assured that our future looks very bright.  The fact that Ryan is tackling such problems should also make us all feel good in that a PhD is not required to innovate in the mobile world.  A person with a strong desire a bit of work can make it happen.

One of the things that Ryan has asked me to help him with seemed like a great topic for this week’s blog post.  His problem, and may be yours, is how to programmatically put an Android phone into airplane mode.  Airplane mode serves to cut all the communications and radio signals of the phone.  No phone calls.  No SMS messages.  No Bluetooth.  All of these services are out when a device is in airplane mode.  In fact, a list of all the affected “radios” that are terminated when a device is in airplane mode can be obtained through the AIRPLANE_MODE_RADIOS constant defined on Settings.System.  The code below demonstrates the use of this API to log the available radios of a device.

Log.i("Test",
        "List of radios:  "
                + Settings.System.getString(getContentResolver(),
                        Settings.System.AIRPLANE_MODE_RADIOS));

First – Get Permission

In order to be able to put the device in or out of airplane mode, the application must have WRITE_SETTINGS user permissions.  This permission allows an application to read or write the system settings that include airplane mode.  To request WRITE_SETTINGS add the <uses-permissions> element to the Android Manifest file as demonstrated below.

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

    <uses-permission android:name="android.permission.WRITE_SETTINGS" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
    <!-- your components here -->
    </application>
</manifest>

Coding Airplane Mode On/Off

Airplane mode is an integer toggle setting.  It is either on (1) or off (0).  Use the putInt() method on Settings.System to change the airplane mode setting.  This method requires a ContentResolver (which can be obtained from the context) and the system setting to be set (which of course is the AIRPLANE_MODE_ON setting).  The code below demonstrates setting it on and off.

Settings.System.putInt(getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 1);  //  turn airplane mode on
Settings.System.putInt(getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON, 0);  // turn airplane mode off

Since it is a toggle, you generally want to know its existing state before turning it on or off.  Use the getInt() method on Settings.System to get the existing value.

Settings.System.getInt(getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);

Broadcast the Airplane Mode Update

After changing the system setting, but before airplane mode takes effect, you must broadcast (via Intent) 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.  Failure to broadcast this intent leaves the device in its current airplane mode setting.

Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.putExtra("state", false);  //indicate the "state" of airplane mode is changed to on
sendBroadcast(intent);

A Clean and Total Example

You’ll find an elegant way to check the setting, toggle the current airplane mode, and broadcast the mode change intent in a post here.  Below, I have borrowed a bit from that post, simplified it a bit, and provided an example activity method that could be setup to toggle airplane mode on a button press (via onClick listener callback).

public void airplaneModeOn(View view) {
    try {
        boolean isEnabled = Settings.System.getInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, 0) == 1;
        Settings.System.putInt(getContentResolver(),Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", !isEnabled);
        sendBroadcast(intent);
    } catch (Exception e) {
        Toast.makeText(this, "exception:" + e.toString(), Toast.LENGTH_LONG).show();
    }
}

The results of executing this method via a simple activity (the button here is setup to trigger the airplaneModeOn method) is shown in the AVD images below.

SNAGHTML53f0355SNAGHTML53e0bce

And when you try to use the phone on the AVD with airplane mode on, you’ll see that the AVD appropriately blocks the call.

SNAGHTML5401d7a

Wrap Up

So I hope this example helps you and my friend Ryan.  The Android API makes working with many of the devices facilities and services very easy.  Remember, if you have a need to learn more about Android, think about taking Intertech’s Complete Android class.  Find out more details about our class here and contact Dan McCabe at 800-866-9884..

Pin It on Pinterest

Share This

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!