9

I want to enable/disable an Android phone's GSM connection. I need to disable/enable calls and SMS as required. How might I do this?

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Ram
  • 91
  • 1
  • 1
  • 3

2 Answers2

5
/* Toggle airplane mode for 1 of the 4 allowed types
 * type allowed values: cell, wifi, bluetooth, nfc
 */
private void changeRadioComponentEnabled(Context context, String type, boolean radio_component_enabled, boolean reset){     
        // now toggle airplane mode from on to off, or vice versa
        Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, radio_component_enabled ? 0 : 1);

        // now change system behavior so that only one component is turned off
        // this also affects the future behavior of the system button for toggling air-plane mode. 
        // to reset it in order to maintain the system behavior, set reset to true, otherwise we lazily make sure mobile voice is always on
        Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, type); 

        // post an intent to reload so the menu button switches to the new state we set
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", radio_component_enabled ? false : true);
        context.sendBroadcast(intent);

        // revert to default system behavior or not
        if (reset){ Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, "cell,bluetooth,wifi,nfc"); }
        // if reset to default is not chosen, always enable mobile cell at  least
        // but only if NOT changing the mobile connection...
        else if (type.indexOf("cell") == 0) { Settings.System.putString(context.getContentResolver(), Settings.System.AIRPLANE_MODE_RADIOS, "cell");}
}//end method

naturally this require the permission android.permission.WRITE_SETTINGS, and for bluetooth android.permission.BLUETOOTH_ADMIN. For NFC you might need android.permission.NFC.

EDITS: heavily modified since original, since I was actually using this in a different way in my own app

slinden77
  • 3,378
  • 2
  • 37
  • 35
  • My phone also has wimax in Settings.System.AIRPLANE_MODE_RADIOS: "cell,bluetooth,wifi,nfc,wimax" . That's surprising because this phone doesn't support 4G WiMax! – pmont Aug 08 '13 at 06:50
5

EDIT: This solution will also turn off wifi, bluetooth, etc...

If you want to turn off radio only, I think it's related to this issue: http://code.google.com/p/android/issues/detail?id=1065 I am really pessimistic about finding a good solution, but curious to see other answers.

See the blog article Android: Controlling Airplane Mode ,

// 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.

Don't forget you need the WRITE_SETTINGS permission to do this, though.

Prof. Falken
  • 24,226
  • 19
  • 100
  • 173
Waza_Be
  • 39,407
  • 49
  • 186
  • 260
  • When you will switch phone to Airplane mode you will also suppress wi-fi and bluetooth. I don't think that it's what is expected. Primary task was to suppress only GSM connection... – Barmaley Sep 09 '11 at 05:21
  • yes.i already tried with airplanemode. That will disable bluetooth also.but i want to disable only call and sms is that possible?????i disabled GPRS. Is there any method is availble???? – Ram Sep 09 '11 at 06:07
  • 2
    Sorry, you are totally right. I don't think there is a "proper" solution, see http://code.google.com/p/android/issues/detail?id=1065, but if you have smart users, you can prompt them the menu (type in *#*#INFO#*#* (*#*#4636#*#*) in the dialer). Will edit my answer – Waza_Be Sep 09 '11 at 08:37