set Airplane Mode On - Android Phone

Android examples for Phone:Airplane Mode

Description

set Airplane Mode On

Demo Code


//package com.java2s;

import android.content.Context;
import android.content.Intent;

import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

public class Main {
    /**/*w  w w . j a v  a 2s.  c  om*/
     * 
     * @param context
     * @return true if AirplaneMode is switched successfully
     * @throws SettingNotFoundException
     */
    public static boolean setAirplaneModeOn(Context context)
            throws SettingNotFoundException {
        if (isAirplaneModeOn(context)) {
            return true;
        }
        boolean success = Settings.System.putInt(
                context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 1);
        if (!success) {
            return false;
        }
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", true);
        context.sendOrderedBroadcast(intent, null);
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
        }
        return success;
    }

    /**
     * @param context
     * @return true if Airplane mode is on
     * @throws SettingNotFoundException
     */
    public static boolean isAirplaneModeOn(Context context)
            throws SettingNotFoundException {
        int value = Settings.System.getInt(context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON);
        return value == 0 ? false : true;
    }
}

Related Tutorials