Switch off Airplane Mode, mobile network and wifi - Android Network

Android examples for Network:Network Operation

Description

Switch off Airplane Mode, mobile network and wifi

Demo Code


//package com.java2s;
import java.lang.reflect.Method;
import android.content.Context;
import android.content.Intent;
import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.telephony.TelephonyManager;
import android.util.Log;

public class Main {
    private static final String DISABLE_DATA_CONNECTION_METHOD = "disableDataConnectivity";
    private static final String ENABLE_DATA_CONNECTION_METHOD = "enableDataConnectivity";
    private static final String I_TELEPHONY_METHOD = "getITelephony";
    private static final String TAG = "NetworkSwitcher";

    /**/*from   ww w  .j  a  va 2 s  . com*/
     * Switch off AirplaneMode, mobile network and wifi
     * 
     * @param context
     * @throws Exception
     */
    public static void setAllPossibleNetworksOn(Context context)
            throws Exception {
        if (isAirplaneModeOn(context)) {
            boolean success = setAirplaneModeOff(context);
            if (!success) {
                throw new IllegalStateException(
                        "AirplaneMode was NOT turned ON");
            }
        }
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        byte numberOfErrors = 0;
        try {
            setNetworkOn(context);
        } catch (Exception e) {
            numberOfErrors++;
        }
        try {
            setWifiOn(context);
        } catch (Exception e) {
            numberOfErrors++;
        }
        if (numberOfErrors == 2) {
            throw new Exception("Wifi and Network are not turned on");
        }
    }

    /**
     * @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;
    }

    /**
     * 
     * @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
     *            - Context
     * @return true if Network switched on successfully
     * @throws SettingNotFoundException
     */
    public static boolean setNetworkOn(Context context)
            throws SettingNotFoundException {
        boolean isEnabled = false;
        if (isNetworkOn(context)) {
            return true;
        }
        isEnabled = changeNetworkState(context,
                ENABLE_DATA_CONNECTION_METHOD);
        return isEnabled;
    }

    /**
     * Switch On Wifi network
     * 
     * @param context
     *            - Context
     * @return true if Wifi successfully switched on
     * @throws SettingNotFoundException
     *             if Wifi settings were not found
     */
    public static boolean setWifiOn(Context context) {
        WifiManager wifiMng = (WifiManager) context
                .getSystemService(Context.WIFI_SERVICE);
        if (wifiMng == null) {
            return false;
        }
        if (wifiMng.isWifiEnabled()) {
            return true;
        }
        Settings.System.putInt(context.getContentResolver(),
                Settings.Secure.WIFI_ON, 1);
        try {
            wifiMng.reassociate();
            wifiMng.reconnect();
            wifiMng.setWifiEnabled(true);
            Settings.System.putInt(context.getContentResolver(),
                    Settings.Secure.WIFI_ON, 1);
            Thread.sleep(5000);
            return wifiMng.isWifiEnabled();
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * @param context
     *            - Context
     * @return true if it is mobile network connection
     */
    public static boolean isNetworkOn(Context context) {
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);

        return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED ? true
                : false;
    }

    private static boolean changeNetworkState(Context context, String method)
            throws SettingNotFoundException {
        TelephonyManager telephonyManager = (TelephonyManager) context
                .getSystemService(Context.TELEPHONY_SERVICE);
        try {
            Method dataConnSwitchmethod = null;
            Class<?> telephonyManagerClass;
            Object ITelephonyStub;
            Class<?> ITelephonyClass;

            telephonyManagerClass = Class.forName(telephonyManager
                    .getClass().getName());
            Method getITelephonyMethod = telephonyManagerClass
                    .getDeclaredMethod(I_TELEPHONY_METHOD);
            getITelephonyMethod.setAccessible(true);
            ITelephonyStub = getITelephonyMethod.invoke(telephonyManager);
            ITelephonyClass = Class.forName(ITelephonyStub.getClass()
                    .getName());
            Log.i(TAG, "changeNetworkState(): " + "ITelephonyStub = "
                    + ITelephonyStub.toString());
            dataConnSwitchmethod = ITelephonyClass
                    .getDeclaredMethod(method);
            Log.i(TAG, "changeNetworkState(): " + "ITelephonyClass = "
                    + ITelephonyClass.getName());
            Log.i(TAG, "changeNetworkState(): " + "dataConnSwitchmethod = "
                    + dataConnSwitchmethod.getName());
            dataConnSwitchmethod.setAccessible(true);
            dataConnSwitchmethod.invoke(ITelephonyStub);
            Thread.sleep(5000);
        } catch (Exception e) {
            Log.i(TAG, "changeNetworkState(): Exception: " + e);
            if (method == DISABLE_DATA_CONNECTION_METHOD) {
                setAirplaneModeOn(context);
            }
        }
        return telephonyManager.getDataState() == TelephonyManager.DATA_CONNECTED;
    }

    /**
     * 
     * @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;
    }
}

Related Tutorials