0

I am just a starter in Android. I have an Android code which has a Button. On click of the button, it should Invoke AirPlane mode and then again back to normal mode. Here is my code :

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // load controls
    tvStatus = (TextView)findViewById(R.id.tvStatus);
    togState = (Button)findViewById(R.id.togState);

    // set click event for button
    togState.setOnClickListener(new OnClickListener() {                     
                    @Override
                    public void onClick(View v) {
                            // check current state first
                            boolean state = isAirplaneMode();
                            // toggle the state
                            toggleAirplaneMode(state);

                            state = isAirplaneMode();
                            // toggle the state
                            toggleAirplaneMode(state);

                    }
            });
}

public void toggleAirplaneMode(boolean state) {
    // toggle airplane mode
    Settings.System.putInt(this.getContentResolver(),Settings.System.AIRPLANE_MODE_ON, state ? 0 : 1);

    // broadcast an intent to inform
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", !state);
    sendBroadcast(intent);
}



public boolean isAirplaneMode() {
    return Settings.System.getInt(this.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0) == 1;
}

}

The problem here is, my phone will go in AirPlane mode and it toggles back also. But this process I cannot stop. Is the problem with the way I handled the OnClick Listener by calling same method (toggleAirplaneMode) twice?

Regards,

trueblue
  • 190
  • 4
  • 17

4 Answers4

2

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);
Community
  • 1
  • 1
Hybrid Developer
  • 2,239
  • 28
  • 49
0

Replace the onClick method with this:

                public void onClick(View v) {
                        // check current state first
                        boolean state = isAirplaneMode();
                        // toggle the state
                        final Handler handler = new Handler(){
                            @Override
                            public void handleMessage(Message msg) {
                                toggleAirplaneMode(!state);
                                super.handleMessage(msg);
                            }       
                        };
                        Thread th = new Thread() {
                           @Override
                           public void run() {
                              toggleAirplaneMode(!state);
                              handler.sendEmptyMessage(0);
                           };
                        };
                        th.start();
                }

Every time you will click the button, it will toggle the airplaneMode. If it doesn't work, try removing !

P Ravikant
  • 989
  • 2
  • 10
  • 27
  • Will it Invoke Flight Mode and then Invoke Normal Mode on a Single Click of a Button? – trueblue Aug 13 '13 at 11:41
  • I mean I want to click only once. I need to invoke Flight mode then back to normal mode by clicking the button only once. The code in Answer one goes to flight mode on click of a button but how to go back to normal mode? – trueblue Aug 13 '13 at 11:44
  • on first click, it will go to flight mode. on second click, it will go to normal mode. – P Ravikant Aug 13 '13 at 11:45
  • Do you want to do both the things simultaneously on a single click? – P Ravikant Aug 13 '13 at 11:46
  • Yes @micro.pravi.. Simultaneously.. First it should toggle to airplane mode then back to normal mode. I want to avoid two clicks. – trueblue Aug 13 '13 at 11:49
  • I am trying @micro.pravi – trueblue Aug 13 '13 at 12:11
  • Thanks for the help. I tried it in my cell. It is going into flight mode. Then it is coming to No Service State. (The broken Memory Card like symbol appearing instead of Signal Bars) – trueblue Aug 13 '13 at 12:15
  • I have declared handler before OnCreate. Since i was getting an error stating : handler.sendEmptyMessage(0); //Local variable handler might not be initialised super.handleMessage(msg); – trueblue Aug 13 '13 at 12:23
  • try removing super.handleMessage(msg) @trueblue – P Ravikant Aug 13 '13 at 12:47
  • Same issue after removing super.handleMessage(msg) – trueblue Aug 13 '13 at 13:15
  • Thanks micro.pravi for your help. I found out the solution and it worked. Have answered it below. – trueblue Aug 14 '13 at 05:23
0

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.

trueblue
  • 190
  • 4
  • 17
  • originally it can not on-off the airplane mode, it just off sim card but after click on that again sim card can notbe visible whts the solutions ?? –  Jun 12 '14 at 07:32
  • Hi nikiIgeniusDev. I am posting my entire code below. Chk out. – trueblue Jul 01 '14 at 04:55
0

Check this out... This might help..

public class MainActivity extends Activity { Context context;

private void changeRadioComponentEnabled(Context paramContext, String paramString, boolean paramBoolean1, boolean paramBoolean2)
  {
    boolean bool = false;
    ContentResolver localContentResolver = paramContext.getContentResolver();
    int i;
    if (!paramBoolean1)
      i = 1;
    else
      i = 0;
    Settings.System.putInt(localContentResolver, "airplane_mode_on", i);
    Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", paramString);
    Intent localIntent = new Intent("android.intent.action.AIRPLANE_MODE");
    if (!paramBoolean1)
      bool = true;
    localIntent.putExtra("state", bool);
    paramContext.sendBroadcast(localIntent);
    if (!paramBoolean2)
    {
      if (paramString.indexOf("cell") == 0)
        Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell");
    }
    else
      Settings.System.putString(paramContext.getContentResolver(), "airplane_mode_radios", "cell,bluetooth,wifi,nfc");
  }




@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    this.context = this;
    ((Button)findViewById(R.id.button1)).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
        MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", false, false);
      }
    });

    ((Button)findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener()
    {
      public void onClick(View paramAnonymousView)
      {
          MainActivity.this.changeRadioComponentEnabled(MainActivity.this.context, "cell", true, false);
      }
    });



}
trueblue
  • 190
  • 4
  • 17