How to Set the Airplane_Mode_On to "True" or On

How to set the AIRPLANE_MODE_ON to True or ON?

See the blog article Android: Controlling Airplane Mode ,

Works only upto API 16

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

where isEnabled is whether airplane mode is enabled or not.

Toggle airplane mode in Android

This answer contains code necessary to do this. Also make sure you have the WRITE_SETTINGS permission.

Adapted from Controlling Airplane Mode:

// read the airplane mode setting
boolean isEnabled = Settings.System.getInt(
getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;

// toggle airplane mode
Settings.System.putInt(
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);

android - Settings.Global#AIRPLANE_MODE_ON - Field requires API level 17 (current min is 16)

Settings.Global.AIRPLANE_MODE_ON was only introduced in API level 17, while your app is configured to run on API level 16 or newer.

Edit your AndroidManifest.xml and set the minSdkVersion to 17.

Modify AIRPLANE_MODE_ON on Android 4.2 (and above)

I know you explicitly ask for a solution without rooting, but if you have a rooted device there is at least the (kind of) official solution to move your app into folder /system/app. Then it is possible to write into Settings.Global:

Settings.Global.putInt(context.getContentResolver(), Global.AIRPLANE_MODE_ON, mode ? 1 : 0);

Have a look of how to toggle airplane mode for 4.2
and how to move your app into /system/app here. I use this for my toggle widget Mini Status (source code link).

Enable and Disable Airplane Mode successively Android

I got it finally

I used this in my code

            public void onClick(View v) {
// check current state first
boolean state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);

state = isAirplaneMode();
// toggle the state
toggleAirplaneMode(state);
ser = new ServiceState();
ser.setState(STATE_IN_SERVICE);
}

And I have declared final int STATE_IN_SERVICE = 0; before OnCreate. And ser is the instance of ServiceState.

Thank you for your replies.

Settings.system.putInt() for Flight mode is not working

You need to send Broadcast with Intent.ACTION_AIRPLANE_MODE_CHANGED change your code as:

boolean isEnabled = Settings.System.getInt(
paramContext.getContentResolver(),
Settings.System.AIRPLANE_MODE_ON, 0) == 1;
if(isEnabled==true)
{
Settings.System.putInt(
paramContext.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);
paramContext.sendBroadcast(intent);
}
else
{
Settings.System.putInt(
paramContext.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);
paramContext.sendBroadcast(intent);
}

NOTE ::-
MAKE SURE YOU HAVE THESE PERMISSIONS IN manifest.xml :

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


Related Topics



Leave a reply



Submit