How can android tell if it is currently in airplane mode

  • 2020-05-07 20:23:52
  • OfStack

How to determine whether the system is currently in flight mode in Android:
 
public static boolean IsAirModeOn(Context context) { 
return (Settings.System.getInt(context.getContentResolver(), 
Settings.System.AIRPLANE_MODE_ON, 0) == 1 ? true : false); 
} 

How do I switch flight mode
 
public static void setAirplaneMode(Context context, boolean enabling) { 
Settings.System.putInt(context.getContentResolver(), 
Settings.System.AIRPLANE_MODE_ON, enabling ? 1 : 0); 
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED); 
intent.putExtra("state", enabling); 
context.sendBroadcast(intent); 
} 

How to register and cancel the automatic flight time
registered
 
AlarmManager am = (AlarmManager) context 
.getSystemService(Context.ALARM_SERVICE); 
Intent intent = new Intent(AIR_ALERT_ACTION); 
Parcel out = Parcel.obtain(); 
air.writeToParcel(out, 0); 
out.setDataPosition(0); 
intent.putExtra(AIR_RAW_DATA, out.marshall()); 
PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 
PendingIntent.FLAG_CANCEL_CURRENT); 
am.set(AlarmManager.RTC_WAKEUP, atTimeInMillis, sender); cancel  
AlarmManager am = (AlarmManager) context 
.getSystemService(Context.ALARM_SERVICE); 
endingIntent sender = PendingIntent.getBroadcast(context, 0, 
new Intent(action), PendingIntent.FLAG_CANCEL_CURRENT); 
am.cancel(sender); 

How to control flight mode switching hardware (cell,Bluetooth,wifi)
 
Settings.System.putString(context.getContentResolver(), 
Settings.System.AIRPLANE_MODE_RADIOS, air_mode_radios);air_mode_radios for 1 A string like this. Look android In the source android/provider/Settings.java 

/*** 
* Whether Airplane Mode is on. 
*/ 
public static final String AIRPLANE_MODE_ON = "airplane_mode_on"; 
/*** 
* Constant for use in AIRPLANE_MODE_RADIOS to specify Bluetooth radio. 
*/ 
public static final String RADIO_BLUETOOTH = "bluetooth"; 
/*** 
* Constant for use in AIRPLANE_MODE_RADIOS to specify Wi-Fi radio. 
*/ 
public static final String RADIO_WIFI = "wifi"; 
/*** 
* Constant for use in AIRPLANE_MODE_RADIOS to specify Cellular radio. 
*/ 
public static final String RADIO_CELL = "cell"; 
/*** 
* A comma separated list of radios that need to be disabled when airplane mode 
* is on. This overrides WIFI_ON and BLUETOOTH_ON, if Wi-Fi and bluetooth are 
* included in the comma separated list. 
*/ 
public static final String AIRPLANE_MODE_RADIOS = "airplane_mode_radios"; 
/*** 
* A comma separated list of radios that should to be disabled when airplane mode 
* is on, but can be manually reenabled by the user. For example, if RADIO_WIFI is 
* added to both AIRPLANE_MODE_RADIOS and AIRPLANE_MODE_TOGGLEABLE_RADIOS, then Wifi 
* will be turned off when entering airplane mode, but the user will be able to reenable 
* Wifi in the Settings app. 
* 
* {@hide} 
*/ 
public static final String AIRPLANE_MODE_TOGGLEABLE_RADIOS = "airplane_mode_toggleable_radios";

If air_mode_radios= "cell,bluetooth,wifi", this is to switch flight mode is to switch the cell,bluetooth,wifi hardware in the string, we can set the value of the string, to control whether the three hardware in the switching flight mode is switching state.

Related articles: