WiFi state is not enabling

淺唱寂寞╮ 提交于 2019-12-01 03:58:01

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

Download this example it is what you want

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!