WiFi status does not allow

I am trying to create a widget to enable and disable Wi-Fi.

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 that I use to disconnect, it works fine, but the resolving part is not working fine. Shortly after turning on Wi-Fi, I print wifi state, I get it as false.

+7
source share
2 answers

Here's how to enable and disable Wi-Fi in Android.

First you need to declare the following in the 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 that in the class Activity

 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 Wifi service from our system

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

Check that our Wi-Fi is on or off

 if(wifiManager.isWifiEnabled()){ 

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

Link

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

+9
source

Download this example, this is what you want.

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

0
source

All Articles