Example usage for android.content Intent ACTION_AIRPLANE_MODE_CHANGED

List of usage examples for android.content Intent ACTION_AIRPLANE_MODE_CHANGED

Introduction

In this page you can find the example usage for android.content Intent ACTION_AIRPLANE_MODE_CHANGED.

Prototype

String ACTION_AIRPLANE_MODE_CHANGED

To view the source code for android.content Intent ACTION_AIRPLANE_MODE_CHANGED.

Click Source Link

Document

Broadcast Action: The user has switched the phone into or out of Airplane Mode.

Usage

From source file:Main.java

@SuppressWarnings("deprecation")
private static void closeBelowApiLevel17(Context context) throws Exception {
    Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", false);
    context.sendBroadcast(intent);//from w w  w  .  j a v  a 2  s  . c  om
}

From source file:Main.java

public static void setAirplaneMode(Context context, boolean newStatus) {
    boolean airplaneModeOn = isAirplaneModeOn(context);

    if (airplaneModeOn && newStatus) {
        return;/*  w ww .  j a  v a  2s . co  m*/
    }
    if (!airplaneModeOn && !newStatus) {
        return;
    }
    if (airplaneModeOn && !newStatus) {
        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);
        return;
    }
    if (!airplaneModeOn && newStatus) {

        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);
        return;
    }
}

From source file:Main.java

@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
public static void toggleAirplane(Context context) {

    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.ICE_CREAM_SANDWICH_MR1)
        return;//  ww w  .  ja  v  a 2  s .c om

    int state = 0;
    try {
        if (isJBean())
            state = Settings.System.getInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON);
        else
            state = Settings.System.getInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON);
    } catch (SettingNotFoundException e) {
        e.printStackTrace();
    }

    if (state == 0) {
        if (isJBean())
            Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 1);
        else
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", true);
        context.sendBroadcast(intent);
    } else {
        if (isJBean())
            Settings.System.putInt(context.getContentResolver(), Settings.Global.AIRPLANE_MODE_ON, 0);
        else
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", false);
        context.sendBroadcast(intent);
    }
}

From source file:org.fairphone.peaceofmind.PeaceOfMindBroadCastReceiver.java

@Override
public void onReceive(Context context, Intent intent) {
    mContext = context;/*from   ww w  .j a  va 2  s  .  c  o m*/
    setupDeviceController();

    String action = intent.getAction();
    if (action != null) {
        // obtains the piece of mind data from shared preferences
        mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);

        mCurrentStats = PeaceOfMindStats.getStatsFromSharedPreferences(mSharedPreferences);

        if (action.equals(PeaceOfMindActivity.UPDATE_PEACE_OF_MIND)) {
            updateTargetTime(intent);
        } else if (Intent.ACTION_AIRPLANE_MODE_CHANGED.equals(action)) {
            //only react to this if the app is running
            if (mCurrentStats.mIsOnPeaceOfMind) {
                Bundle extras = intent.getExtras();
                //if the intent was sent by the system end Peace of mind
                if (!extras.containsKey(AirplaneModeToggler.PEACE_OF_MIND_TOGGLE)) {
                    endPeaceOfMind(true);
                }
            }
        } else if (Intent.ACTION_SHUTDOWN.equals(action) && mCurrentStats.mIsOnPeaceOfMind) {
            endPeaceOfMind(true);
        } else {
            performTimeTick();
        }
    } else {
        return;
    }
    // update the widgets
    updateWidget(context);

}

From source file:sssemil.com.wifiapmanager.MainService.java

@Override
public void onCreate() {
    super.onCreate();

    mContext = this;

    IntentFilter intentFilter = new IntentFilter("android.net.wifi.WIFI_AP_STATE_CHANGED");
    intentFilter.addAction("android.net.conn.TETHER_STATE_CHANGED");
    intentFilter.addAction(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);

    mIcon = R.drawable.ic_tt;// ww  w.java  2s.  com

    mContext.registerReceiver(mReceiver, intentFilter);

    mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

    mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);

    mSharedPreferences.registerOnSharedPreferenceChangeListener(this);

    mLooper = getMainLooper();
    mScanThread = new HandlerThread("WifiClientScanner");
    if (!mScanThread.isAlive()) {
        mScanThread.start();
        mScanHandler = new WifiClientScanner(mScanThread.getLooper());
        mScanHandler.sendEmptyMessage(0);
    }
}

From source file:uk.ac.ucl.excites.sapelli.util.DeviceControl.java

/**
 * Set AirplaneMode/*  w  ww  . jav a 2 s. com*/
 * 
 * @param context
 * @param enabled
 *            (True to set the device in Airplane Mode)
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("deprecation")
private static void setAirplaneMode(Context context, boolean enabled) {
    try {
        // If airplane mode is on, value 0, else value is 1
        if (canToogleAirplaneMode()) {
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    enabled ? 1 : 0);

            // Reload when the mode is changed each time by sending Intent
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", enabled);
            context.sendBroadcast(intent);

            Debug.d("Airplane mode is: " + (enabled ? "ON" : "OFF"));
        }
    } catch (Exception e) {
        Debug.e("Error upon toggling airplane more.", e);
    }
}

From source file:edu.cmu.cylab.starslinger.Service.java

@Override
public void onCreate() {
    super.onCreate();

    mIsRunning = true;//from  w  w w . j  av a  2s .  c o m

    registerReceiver(mAirplaneModeReceiver, new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED));
}

From source file:uk.ac.ucl.excites.sapelli.shared.util.android.DeviceControl.java

/**
 * Set AirplaneMode//from ww  w .  ja v a  2 s .  co  m
 * 
 * @param context
 * @param enabled pass {@code true} to put the device in airplane mode, and {@code false} to leave airplane mode
 */
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
@SuppressWarnings("deprecation")
private static void setAirplaneMode(Context context, boolean enabled) {
    try {
        // If airplane mode is on, value 0, else value is 1
        if (canSetAirplaneMode()) {
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    enabled ? 1 : 0);

            // Reload when the mode is changed each time by sending Intent
            Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            intent.putExtra("state", enabled);
            context.sendBroadcast(intent);

            Debug.d("Airplane mode is: " + (enabled ? "ON" : "OFF"));
        }
    } catch (Exception e) {
        Debug.e("Error upon toggling airplane more.", e);
    }
}

From source file:net.geniecode.ttr.ScheduleReceiver.java

@SuppressWarnings("deprecation")
@SuppressLint({ "Recycle", "NewApi", "InlinedApi" })
@Override/* w  w  w. j a va2 s. c o m*/
public void onReceive(Context context, Intent intent) {
    if (!Schedules.SCHEDULE_ACTION.equals(intent.getAction())) {
        // Unknown intent, bail.
        return;
    }

    Schedule schedule = null;
    // Grab the schedule from the intent. Since the remote AlarmManagerService
    // fills in the Intent to add some extra data, it must unparcel the
    // Schedule object. It throws a ClassNotFoundException when unparcelling.
    // To avoid this, do the marshalling ourselves.
    final byte[] data = intent.getByteArrayExtra(Schedules.SCHEDULE_RAW_DATA);
    if (data != null) {
        Parcel in = Parcel.obtain();
        in.unmarshall(data, 0, data.length);
        in.setDataPosition(0);
        schedule = Schedule.CREATOR.createFromParcel(in);
    }

    if (schedule == null) {
        // Make sure we set the next schedule if needed.
        Schedules.setNextSchedule(context);
        return;
    }

    // Disable this schedule if it does not repeat.
    if (!schedule.daysOfWeek.isRepeatSet()) {
        Schedules.enableSchedule(context, schedule.id, false);
    } else {
        // Enable the next schedule if there is one. The above call to
        // enableSchedule will call setNextSchedule so avoid calling it twice.
        Schedules.setNextSchedule(context);
    }

    long now = System.currentTimeMillis();

    if (now > schedule.time + STALE_WINDOW) {
        return;
    }

    // Get telephony service
    mTelephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

    // Execute only for devices with versions of Android less than 4.2
    if (android.os.Build.VERSION.SDK_INT < 17) {
        // Get flight mode state
        boolean isEnabled = Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0) == 1;

        // Get Wi-Fi service
        mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

        if ((schedule.aponoff) && (!isEnabled) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)) {
            // Enable flight mode
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    isEnabled ? 0 : 1);

            // Get Wi-Fi state and disable that one too, just in case
            // (On some devices it doesn't get disabled when the flight mode is
            // turned on, so we do it here)
            boolean isWifiEnabled = mWifiManager.isWifiEnabled();

            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);

            if (isWifiEnabled) {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(WIFI_STATE, isWifiEnabled);
                editor.commit();
                mWifiManager.setWifiEnabled(false);
            } else {
                SharedPreferences.Editor editor = settings.edit();
                editor.putBoolean(WIFI_STATE, isWifiEnabled);
                editor.commit();
            }

            // Post an intent to reload
            Intent relintent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            relintent.putExtra("state", !isEnabled);
            context.sendBroadcast(relintent);
        } else if ((!schedule.aponoff) && (isEnabled) && (schedule.mode.equals("1"))) {
            // Disable flight mode
            Settings.System.putInt(context.getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    isEnabled ? 0 : 1);

            // Restore previously remembered Wi-Fi state
            SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
            Boolean WiFiState = settings.getBoolean(WIFI_STATE, true);

            if (WiFiState) {
                mWifiManager.setWifiEnabled(true);
            }

            // Post an intent to reload
            Intent relintent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
            relintent.putExtra("state", !isEnabled);
            context.sendBroadcast(relintent);
        }
        // Check whether there are ongoing phone calls, and if so
        // show notification instead of just enabling the flight mode
        else if ((schedule.aponoff) && (!isEnabled) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE)) {
            setNotification(context);
        }
        // Execute for devices with Android 4.2   or higher
    } else {
        // Get flight mode state
        String result = Settings.Global.getString(context.getContentResolver(),
                Settings.Global.AIRPLANE_MODE_ON);

        if ((schedule.aponoff) && (result.equals("0")) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() == TelephonyManager.CALL_STATE_IDLE)) {
            // Keep the device awake while enabling flight mode
            Intent service = new Intent(context, ScheduleIntentService.class);
            startWakefulService(context, service);
        } else if ((!schedule.aponoff) && (result.equals("1")) && (schedule.mode.equals("1"))) {
            // Keep the device awake while enabling flight mode
            Intent service = new Intent(context, ScheduleIntentService.class);
            startWakefulService(context, service);
        } else if ((schedule.aponoff) && (result.equals("0")) && (schedule.mode.equals("1"))
                && (mTelephonyManager.getCallState() != TelephonyManager.CALL_STATE_IDLE)) {
            setNotification(context);
        }
    }

    // Get audio service
    mAudioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

    // Get current ringer mode and set silent or normal mode accordingly
    switch (mAudioManager.getRingerMode()) {
    case AudioManager.RINGER_MODE_SILENT:
        if ((!schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
        break;
    case AudioManager.RINGER_MODE_NORMAL:
        if ((schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
        break;
    case AudioManager.RINGER_MODE_VIBRATE:
        // If the phone is set to vibrate let's make it completely silent
        if ((schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
        }
        // or restore the regular sounds on it if that's scheduled
        else if ((!schedule.silentonoff) && (schedule.mode.equals("2"))) {
            mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
        }
        break;
    }
}

From source file:com.compal.wifitest.WifiTestCase.java

public void enableAirplaneMode(boolean isAirplaneModeBeOn) {
    if (isAirplaneModeBeOn)
        Settings.System.putInt(getActivity().getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 1);
    else//from www. j  av a2  s. co m
        Settings.System.putInt(getActivity().getContentResolver(), Settings.System.AIRPLANE_MODE_ON, 0);

    Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
    intent.putExtra("state", true);
    getActivity().sendBroadcast(intent);

    long startTime = System.currentTimeMillis();
    long tempTime = startTime; ///debug
    while ((System.currentTimeMillis() - startTime) < LONG_TIMEOUT) {
        if (isAirplaneModeBeOn) {
            if (Settings.System.getInt(getActivity().getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    0) != 0) {
                Log.i(tag, "Settings.System.AIRPLANE_MODE_ON, 0) != 0 : on");
                return;
            }
        } else {
            if (Settings.System.getInt(getActivity().getContentResolver(), Settings.System.AIRPLANE_MODE_ON,
                    0) == 0) {
                Log.i(tag, "Settings.System.AIRPLANE_MODE_ON, 0) == 0 : off");
                return;
            }
        }
        sleep(100);
    }
    Log.i(tag, "airplane on/off fail");
}