As @eggyal mentioned toggling Airplane Mode cannot be done from version 4.2 and up.
But what we can do is turn each of the wireless services:
Wifi can be controlled using WifiService ~ getSystemService(Context.WIFI_SERVICE).
Bluetooth can be controlled using BluetoothAdapter- getSystemService(Context.BLUETOOTH_SERVICE).
After some research I found that using Java Reflection you can still control the Android Radios (Wifi, Network, Bluetooth) what Airplane Mode basically does is toggle the state of Bluetooth, Wifi and Networks.
So by controlling the Radios you can in effect create your own Airplane Mode.
WARNING: Using Reflection might fail on some devices (depends on manufacturer implementation of the classes).
This sample code toggles Mobile Network:
private void setMobileRadioEnabled(boolean enabled) {
try {
final ConnectivityManager conman = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final Class conmanClass = Class.forName(conman.getClass().getName());
final Field iConnectivityManagerField = conmanClass.getDeclaredField("mService");
iConnectivityManagerField.setAccessible(true);
final Object iConnectivityManager = iConnectivityManagerField.get(conman);
final Class iConnectivityManagerClass = Class.forName(iConnectivityManager.getClass().getName());
final Method setRadio = iConnectivityManagerClass.getDeclaredMethod("setRadio", Integer.TYPE , Boolean.TYPE);
setRadio.setAccessible(true);
for (NetworkInfo networkInfo : conman.getAllNetworkInfo()) {
if(isNetworkTypeMobile(networkInfo.getType())) {
setRadio.invoke(iConnectivityManager, networkInfo.getType(), enabled);
}
}
} catch (Exception e) {
Log.e(TAG, "Opss...", e);
}
}
public static boolean isNetworkTypeMobile(int networkType) {
switch (networkType) {
case ConnectivityManager.TYPE_MOBILE:
case ConnectivityManager.TYPE_MOBILE_MMS:
case ConnectivityManager.TYPE_MOBILE_SUPL:
case ConnectivityManager.TYPE_MOBILE_DUN:
case ConnectivityManager.TYPE_MOBILE_HIPRI:
case 10:
case 11:
case 12:
case 14:
return true;
default:
return false;
}
}