4

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?

Harsha
  • 400
  • 1
  • 5
  • 16
  • 2
    Duplicate of many http://stackoverflow.com/questions/5533881/toggle-airplane-mode-in-android <- Should have what you're looking for – bschultz Mar 06 '12 at 16:00
  • What's the error on Samsung Galaxy Y when you run this code? Are you getting an exception? Can you share the logcat output? – Anton Cherkashyn Mar 10 '13 at 04:43

2 Answers2

1

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.

Alex
  • 5,971
  • 11
  • 42
  • 80
  • Hi, Thanks for the answer, 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? – Harsha Mar 06 '12 at 17:23
0

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.
Brianjs
  • 2,079
  • 2
  • 20
  • 32