Harsha
Harsha

Reputation: 400

How to Enable/Disable flight mode in android?

I am writing an android app, which needs to disable cell signals of the phone for a specific time period and need to enable the signal back. Can someone pls help me on this?

Thanks in advance.


Hi, Thanks for the below answers, this is working for the virtual device. But this doesn't work for the real device(Samsung Galaxy y) :( , pls someone can tell , why is that?

Upvotes: 4

Views: 8455

Answers (2)

Alex
Alex

Reputation: 6159

try this:

public void onClick(View v){
        context = getApplicationContext();
    if (((ToggleButton)v).isChecked()){
        boolean isEnabled = Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;
        if(isEnabled == false)
        {
        Settings.System.putInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON,1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", 1);
        context.sendBroadcast(intent);
        }



        }else
        {

            Settings.System.putInt(context.getContentResolver(),
                    Settings.System.AIRPLANE_MODE_ON,0);
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", 0);
            context.sendBroadcast(intent);

        }

};
});

In the else part of the code change the number one to number 0.

Upvotes: 1

Brianjs
Brianjs

Reputation: 2339

Look at this page here

http://dustinbreese.blogspot.com/2009/04/andoid-controlling-airplane-mode.html

// Toggle airplane mode.

  Settings.System.putInt(
      context.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);
where isEnabled is whether airplane mode is enabled or not.

Upvotes: 0

Related Questions