Switched Off all types of connection - Android Network

Android examples for Network:Network Connection

Description

Switched Off all types of connection

Demo Code


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 I_TELEPHONY_METHOD = "getITelephony";
    private static final String TAG = "NetworkSwitcher";
    /**//from w  w w . j a va  2 s .c o m
     * Switched Off all types of connection
     * 
     * @param context
     * @throws NetworkSwitchException
     * @throws SettingNotFoundException
     */
    public static void setAllPossibleNetworksOff(Context context)
            throws SettingNotFoundException {
        setWifiOff(context);
        try {
            waitForWifiOff(context);
        } catch (Exception e1) {
            Log.e(TAG, e1.getMessage());

            setNetworkOff(context);
            try {
                Thread.sleep(6000);
            } catch (Exception e) {
                // ignore
            }
        }
    }
    /**
     * Switch off Wifi network
     * 
     * @param context
     *            - Context
     * @return true if Wifi was switched off successfully
     * @throws SettingNotFoundException
     *             if Wifi settings were not found
     */
    @SuppressWarnings("deprecation")
    public static boolean setWifiOff(Context context)
            throws SettingNotFoundException {
        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, 0);
        try {
            wifiMng.setWifiEnabled(false);
            Settings.System.putInt(context.getContentResolver(),
                    Settings.Secure.WIFI_ON, 0);
            return !wifiMng.isWifiEnabled();
        } catch (Exception e) {
            return false;
        }
    }
    private static void waitForWifiOff(Context context) throws Exception {
        boolean isWifiOff = false;
        long startTime = System.currentTimeMillis();
        do {
            Thread.sleep(500);
            isWifiOff = !isWifiOn(context);
        } while (!isWifiOff
                && (System.currentTimeMillis() - startTime < 10000));
        if (!isWifiOff) {
            throw new Exception("Wifi is not turned off");
        }
    }
    /**
     * @param context
     *            - Context
     * @return true if Network switched off successfully
     * @throws SettingNotFoundException
     */
    public static boolean setNetworkOff(Context context)
            throws SettingNotFoundException {
        boolean isEnabled = true;
        if (!isNetworkOn(context)) {
            return true;
        }
        isEnabled = changeNetworkState(context,
                DISABLE_DATA_CONNECTION_METHOD);
        return !isEnabled;
    }
    /**
     * Verify is Wifi network on now
     * 
     * @param context
     *            - Context
     * @return true if Wifi is switched on
     * @throws SettingNotFoundException
     */
    public static boolean isWifiOn(Context context)
            throws SettingNotFoundException {
        int value = Settings.System.getInt(context.getContentResolver(),
                Settings.Secure.WIFI_ON);
        return value == 0 ? false : true;
    }
    /**
     * @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 {

        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