Toggle Airplane Mode in Android

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 airplane mode change

cmd code

public static final boolean execute(String cmd) {
try {
if (cmd != null && cmd.length() > 0) {
Process p = Runtime.getRuntime().exec("su");
DataOutputStream dos = new DataOutputStream(p.getOutputStream());
dos.writeBytes(cmd + "\n");
dos.writeBytes("exit\n");
dos.flush();
dos.close();
p.waitFor();
} else {
Log.e(TAG, "command is null or empty");
}
} catch (IOException ex) {
Log.e(TAG, "IOException");
ex.printStackTrace();
} catch (SecurityException ex) {
Log.e(TAG, "SecurityException");
ex.printStackTrace();
} catch (Exception ex) {
Log.e(TAG, "Generic Exception");
ex.printStackTrace();
}
return false;
}

main

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

int second = 50;

new CountDownTimer(second * 1000, second * 1000) {
public void onTick(long l) {
}

public void onFinish() {
RootPrivileges.execute("settings put global airplane_mode_on 1;" +
"am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true;");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
RootPrivileges.execute("settings put global airplane_mode_on 0;" +
"am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false;");
start();
}
}.start();
}
}

simple code
- Resolved

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.

Toggling Airplane mode on Android 6.0

this is not available for non system applications anymore , because Google denied this on API level. It's possible for data connection to be enabled/disabled on rooted phones tho

How to toggle Airplane Mode on Android 4.2 and above using root?

There is a new "settings" binary in Android 4.2
You can also use it without su, then your app needs the permission required to change this setting declared in your app's manifest (which would be WRITE_SECURE_SETTINGS for flight mode in 4.2 - which is only granted to apps installed on system partition).

Activate

su 
settings put global airplane_mode_on 1
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state true

Deactivate

su
settings put global airplane_mode_on 0
am broadcast -a android.intent.action.AIRPLANE_MODE --ez state false

For other android versions and other settings (flight mode can be activated without root before 4.2) you can use sql injects in settings.db

su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
insert into global values(null, 'airplane_mode_on', 1);

Replace "global" with "system" or "secure" and 'airplane_mode_on' with the key of your desired table entry. For some settings you need to send certain broadcasts afterwards, see example above for flight mode.

To explore your settings.db run this in a terminal app:

su
sqlite3 /data/data/com.android.providers.settings/databases/settings.db
.tables
select * from global
select * from secure
select * from system


Related Topics



Leave a reply



Submit