Switch off Wifi network - Android Network

Android examples for Network:Network Operation

Description

Switch off Wifi network

Demo Code


//package com.java2s;

import android.content.Context;

import android.net.wifi.WifiManager;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;

public class Main {
    /**/*  w ww  .  j ava  2  s . c om*/
     * 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;
        }
    }
}

Related Tutorials