0

I am making an app in which I want to display a pop up if airplane mode is active but when I use the following code, it switches my app to the airplane mode means my device goes to airplane mode. Where as I want if airplane mode is active, a device should give a pop-up only. My code is as follows:

public void airplane() {
    final boolean isEnabled = Settings.System.getInt(
              getContentResolver(), 
              Settings.System.AIRPLANE_MODE_ON, 1) == 0;

        // toggle airplane mode
        Settings.System.putInt(
              getContentResolver(),
              Settings.System.AIRPLANE_MODE_ON, isEnabled ? 0: 1);


        // Post an intent to reload
    //  Toast tt=Toast.makeText(getApplicationContext(), "Your phone is in airplane mode", Toast.LENGTH_SHORT);
    //  tt.show();
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", isEnabled);
        sendBroadcast(intent);

        IntentFilter intentFilter = new IntentFilter("android.intent.action.SERVICE_STATE");

        BroadcastReceiver receiver = new BroadcastReceiver() {
              @Override
              public void onReceive(Context context, Intent intent) {
                    Log.d("AirplaneMode", "Service state changed");
              }
        };

        context.registerReceiver(receiver, intentFilter);

   }
Yury
  • 20,618
  • 7
  • 58
  • 86

1 Answers1

0

At a guess I'd say your issue lies in the fact your integer to boolean code is incorrect.

Try:

final boolean isEnabled = (Settings.System.getInt(
              getContentResolver(), 
              Settings.System.AIRPLANE_MODE_ON, 1)) != 0;
deed02392
  • 4,799
  • 2
  • 31
  • 48
  • It would do that, if it isn't already in airplane mode. That is the purpose of the 'toggle airplane mode' section. Remove that bit if you don't want to toggle the airplane mode state. – deed02392 Mar 13 '12 at 22:02