0

I want to turn airplane mode on and off with the help of a button. How can I turn airplane mode on and off in Android?

manifest:

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

mainactivity:

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    button = findViewById(R.id.button);
    updateUI(isAirPlaneMode());
}
public void flightToggle(View view) {
    boolean state = isAirPlaneMode();
    toggleAirPlaneMode(state);
    updateUI(!state);
    Toast.makeText(this, state + "", Toast.LENGTH_SHORT).show();
}
public void toggleAirPlaneMode(boolean state) {
    Settings.System.putInt(this.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, state ? 0 : 1);
}
public void updateUI(boolean state){
    if (state){
        button.setText(TURN_OFF);
    } else {
        button.setText(TURN_ON);
    }
}
public boolean isAirPlaneMode(){
    return Settings.System.getInt(this.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0) == 1;
}
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Ömer FNz
  • 9
  • 3
  • Maybe it's not possible directly. look at this answer here: https://stackoverflow.com/a/13766921/9715339 – Mayur Gajra Mar 06 '21 at 13:39
  • it looks like a question that is available already if you search it. Please go through this link before asking a question as you are new to the community.https://stackoverflow.com/help/how-to-ask – Anurag Sharma Mar 06 '21 at 18:51
  • 1
    Does this answer your question? [Toggle airplane mode in Android](https://stackoverflow.com/questions/5533881/toggle-airplane-mode-in-android) – AimiKass Mar 15 '21 at 11:09
  • no. i dont find yet – Ömer FNz Mar 15 '21 at 19:16

0 Answers0