8

I am trying to create a widget for enabling and disabling the wifi.

if(myWifiManager.isWifiEnabled()){
            System.out.println("Toggle Wifi Enabled going to disable");
            myWifiManager.setWifiEnabled(false);
        }
        else{
            System.out.println("Wifi Disabled going to enable ");

            myWifiManager.setWifiEnabled(true);
            System.out.println("WI: "+myWifiManager.isWifiEnabled());
        }

This is the code i am using the disabling part is working fine but the enabling part is not working fine. Soon after enabling the wifi i am printing the wifi state i am getting it as false.

3 Answers 3

9

Here is how to turn on and turn off wifi in android.

First you need to declare the following in your manifest file

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.WAKE_LOCK"></uses-permission>

After doing it that on your Activity class

private WifiManager wifiManager;
@Override 
public void onCreate(Bundle icicle) {
 ....................
 wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
 if(wifiManager.isWifiEnabled()){
 wifiManager.setWifiEnabled(false);
 }else{
wifiManager.setWifiEnabled(true);
}

}

Explanation

Get the Wifi service from our system

wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

Check the our wifi is currently turned on or turned off

if(wifiManager.isWifiEnabled()){

Turn on/off our wifi wifiManager.setWifiEnabled();

Reference

WifiEnabler

http://google-androidlovers.blogspot.com/2012/01/scan-for-wireless-networks-in-android.html

http://www.java2s.com/Open-Source/Android/android-platform-apps/Settings/com/android/settings/wifi/WifiApEnabler.java.htm

2
  • 4
    UPDATE_DEVICE_STATS is for system application only
    – Superbiji
    Dec 9, 2014 at 3:56
  • Aside from UPDATE_DEVICE_STATS (which @Superbiji is right, should not be used) the other permissions are normal level. So Android won't yell at you if you try to do something which requires those permissions. IIRC you won't even see anything in logcat.
    – tir38
    Apr 1, 2017 at 19:47
1

Deprecated: Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting android.os.Build.VERSION_CODES#Q or above, this API will always fail and return false. If apps are targeting an older SDK (android.os.Build.VERSION_CODES#P or below), they can continue to use this API.

(Source)

So this will only work for devices with android Pie and below.

0

Download this example it is what you want

https://github.com/siddhpuraamitr/WIfi-Toggle-Widget

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.