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.
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.
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.
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.
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.
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).
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.
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.
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..