Skip to content

Instantly share code, notes, and snippets.

@prashantvc
Created May 9, 2014 05:12
Show Gist options
  • Save prashantvc/6052f4b9fe3b657f8f5e to your computer and use it in GitHub Desktop.
Save prashantvc/6052f4b9fe3b657f8f5e to your computer and use it in GitHub Desktop.
public class AirplaneModeService
{
public bool Toggle (Context context)
{
bool isEnabled = IsAirplaneModeOn (context);
// Toggle airplane mode.
SetSettings (context, isEnabled ? 1 : 0);
// Post an intent to reload.
Intent intent = new Intent (Intent.ActionAirplaneModeChanged);
intent.PutExtra ("state", !isEnabled);
context.SendBroadcast (intent);
return true;
}
public static bool IsAirplaneModeOn (Context context)
{
if (Build.VERSION.SdkInt < Build.VERSION_CODES.JellyBeanMr1) {
return Settings.System.GetInt (context.ContentResolver,
Settings.System.AirplaneModeOn, 0) != 0;
} else {
return Settings.Global.GetInt (context.ContentResolver,
Settings.Global.AirplaneModeOn, 0) != 0;
}
}
/// <summary>
/// Sets the Airplane mode
/// </summary>
/// <param name="context">Context</param>
/// <param name="value">1 for On; 0 off</param>
public static void SetSettings (Context context, int value)
{
if (Build.VERSION.SdkInt < Build.VERSION_CODES.JellyBeanMr1) {
Settings.System.PutInt (
context.ContentResolver,
Settings.System.AirplaneModeOn, value);
} else {
Settings.Global.PutInt (
context.ContentResolver,
Settings.Global.AirplaneModeOn, value);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment