set Airplane Mode Off - Android Phone

Android examples for Phone:Airplane Mode

Description

set Airplane Mode Off

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 {
    /**// ww w  . j  a  va 2 s  .  co  m
     * 
     * @param context
     * @return true if airplane mode is switched successfully
     * @throws SettingNotFoundException
     */
    public static boolean setAirplaneModeOff(Context context)
            throws SettingNotFoundException {
        if (!isAirplaneModeOn(context)) {
            return true;
        }
        boolean success = Settings.System.putInt(
                context.getContentResolver(),
                Settings.System.AIRPLANE_MODE_ON, 0);
        if (!success) {
            return false;
        }
        Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        intent.putExtra("state", false);
        context.sendOrderedBroadcast(intent, null);
        try {
            Thread.sleep(10000);
        } 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