Sunday, April 19, 2009

Android: Controlling Airplane Mode

I've spent a lot of time looking for how to programatically enable and disable Airplane Mode. It doesn't appear that the 1.1 Android SDK exposes it yet.

Here're some code snippets to help with that.

Check to see if it is enabled or not:

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


To toggle it:

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


To get notifications on state change:

IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.d("AirplaneMode", "Service state changed");
}
}

context.registerReceiver(receiver, intentFilter);



Please share if there's an easier way!

10 comments:

Nitesh Goyal said...

thanx for your support.Its really useful.

Light in dawn... said...

May I know what to indicate for the permission in AndroidManifest.xml

Thanks.

sangorys said...

Thanks for this !

A last question, please : what is "context" here ?

Because with the following code :

public class HelloWorld extends Activity {
boolean isEnabled = Settings.System.getInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;

// 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);
context.sendBroadcast(intent);
}

The error is : "context cannot be resolved"


Thanks, I am a beginner

Dustin said...

The "context" here is this.getApplicationContext() from your activity. Hope this helps.

Dustin said...

Mr Lincoln, sorry for the late response.

The permissions I had to add were android.permission.WRITE_SETTINGS.

I _think_ that is all I had to add to get this working.

I also added some others for additional functionality, so if the above doesn't work, try these: ACCESS_WIFI_STATE, CHANGE_WIFI_STATE, WAKE_LOCK, VIBRATE, ACCESS_FINE_LOCATION, BLUETOOTH, BLUETOOTH_ADMIN.

Jane said...

i have a question.
why i toggle a airplane button on the setting is working fine.
then i toggle the button again to set it off,

Settings.System.putInt(
context.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0 : 1);

"isEnabled = false"

i only can do it for once, and after is not work.
why??????

Thanks for your help :)

RA said...

Add to your manifest
android.permission.WRITE_SETTINGS

1) open androidManifest.xml
2) click Permissions
3) add
4) user premission
5) android.permission.WRITE_SETTINGS

Paladyr said...

It looks like when Airplane Mode is enabled, the Broadcast Receiver receives several notifications before the radio is turned back on. How do see what's going on that's causing the Broadcast Receiver to be called? Would I be checking the state of the cell radio, is that possible? I want to have some code run when the Airplane Mode is finally turned on and the phone is able to send an SMS message. Thanks!

Paladyr said...

Sorry me again, I'm using getDataState of TelephonyManager and the BroadcastReceiver is showing when the phone is disconnect, then onReceive is called several times while it's connecting, but it never enters the onReceive event when it is connected (returning a value of 2). How do I find out when it's connected so that I can send an SMS message?

kumar said...

Hi I am facing a problem. i want fill 9*9 rectangle gird with different colors( each rectangle with different colors) using java(android code) please tell me