Domanda

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,

È stato utile?

Soluzione 3

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.

Altri suggerimenti

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

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 !

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



}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top